Hello all,
I'm having a little bit of trouble understanding something basic and
I have boiled it down to the code below. Also attached is a zip file
containing all 3 files. ( The dom4j SMTP server does not seem to like
zip attachments, so I have changed the extension to .zip.txt. Please
change it back to .zip be able to deflate it )
Why would an XPath search from a first level element (id=1) return
all second level elements(id=2,4,6) and not just the children(id=2,4)
of the node I'm searching from ?
I'm pretty sure I've missed something, but I don't know what. Could
someone take a couple of minutes and explain this to me?
thanks in advance,
PP
-------------new.xml-------
<?xml version="2.0" encoding="UTF-8"?>
<root>
<firstlevel id="1">
<secondlevel id="2">
<thirdlevel id="3"/>
</secondlevel>
<secondlevel id="4"/>
</firstlevel>
<firstlevel id="5">
<secondlevel id="6"/>
</firstlevel>
</root>
---------Main.java---------
import java.io.*;
import java.util.*;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
public class Main {
public Main() {
}
public static void main(String[] args)
throws DocumentException{
Document mapdoc;
SAXReader reader = new SAXReader();
File fMapFile = new File("src/new.xml");
mapdoc = reader.read(fMapFile); // throws
org.dom4j.DocumentException
List OuterList = mapdoc.selectNodes("//firstlevel");
System.out.println("Document:\n" + mapdoc.asXML() + "\n----------");
if(OuterList != null && !OuterList.isEmpty()){
for ( int i=0; i < OuterList.size(); i++ ) {
Node firstlevel = (Node) OuterList.get(i);
System.out.println("FirstLevel:i=" + i + ":\n" +
firstlevel.asXML() + "\n----------");
List InnerList =
firstlevel.selectNodes("//secondlevel");
for ( int j=0; j < InnerList.size(); j++){
Element secondlevel = (Element) InnerList.get(j);
System.out.println("SecondLevel:j=" + j + ":\n" +
secondlevel.asXML() + "\n----------");
System.out.println(secondlevel.attribute("id").getValue());
}
}
}
}
}
------------out.txt----------
Document:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<firstlevel id="1">
<secondlevel id="2">
<thirdlevel id="3"/>
</secondlevel>
<secondlevel id="4"/>
</firstlevel>
<firstlevel id="5">
<secondlevel id="6"/>
</firstlevel>
</root>
----------
FirstLevel:i=0:
<firstlevel id="1">
<secondlevel id="2">
<thirdlevel id="3"/>
</secondlevel>
<secondlevel id="4"/>
</firstlevel>
----------
SecondLevel:j=0:
<secondlevel id="2">
<thirdlevel id="3"/>
</secondlevel>
----------
2
SecondLevel:j=1:
<secondlevel id="4"/>
----------
4
SecondLevel:j=2:
<secondlevel id="6"/>
----------
6
FirstLevel:i=1:
<firstlevel id="5">
<secondlevel id="6"/>
</firstlevel>
----------
SecondLevel:j=0:
<secondlevel id="2">
<thirdlevel id="3"/>
</secondlevel>
----------
2
SecondLevel:j=1:
<secondlevel id="4"/>
----------
4
SecondLevel:j=2:
<secondlevel id="6"/>
----------
6
PK
ka5 src/PK ka5^aû {
src/Main.javaMoÛ0ïòX
yÆìvjÃ¥Àd`(ÐîàÚJFCINÛ
þïü%åÃEÇC¤PôKò¡l,+!5é!QÄïóz®Z#óBîã\ËN£°ý|÷¦9æ|>«êGd,U
6)rø;±î·?¶$J§Ú,96z«%òýý/Hå^
]¬þ-Å/"«KÊõê9£FÁ{]'jm2rU.²
;; Ù-KàôɹIèß"£°Û¤U»é"íJfù?,ðp».³y¨ËÛ
ZádhÉ>kÎÉQiø^k*Ûݲ×e4ÓßDN
dRiFô¸¬íÒ´EãÊ Õ`HvóÀ½TÝmÖf\üýhM⸮ 1¸¾«Ñ£Z~!aèMêXÅÚNH
`J\~X Â'×m¬ð5"oÒ´5K
XWè©î©&è÷õ[+µ¶R7¸´¼Ð"êÙ¹<oâ7]uËñ+çã=åI+ úhaè(<-¢èt,mß
/
Ø»COÌr,.0ÅrÛJv0fáÁôþÍlGrÚ|XÍ
æAh+ÿ²Ú ¸$Ø»©ëÛ¾ÆÑüPK A`5
íu ÷ src/new.xml³±¯ÈÍQ(K-*ÎÌϳU2Ô3PRHÍKÎOÉÌK·U
qÓµP²·ã²)ÊÏ/RiEÅ%9©e©9
)@åJv\
@`S0J%K22äô¡ºô´á0ȤØFa/¦#Lq9Â
^}G PK Za5çSÚÄ Ð src/out.txtsÉO.ÍMÍ+±âå²±¯ÈÍQ(K-*ÎÌϳU2Ô3PRHÍKÎOÉÌK·U
qÓµP²·ª+ÊÏ/ÑiEÅ%9©e©9
)@
[EMAIL PROTECTED] °)NMÎÏKAÈÁdÀ²%EHÆJú0}úHqeVn£°SLq:Å«v}¨táË
¤Â¤Â*ÓÖÀjPùÙÁ`
fAJ%÷ Ûbn!6 EÖg®Ï>3}fh`%Håh PK
ka5 src/PK ka5^aû {
"