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
ka†5src/PKka†5^ˆa™û{
src/Main.java•”MoÛ0†ïòX
yÆìvj–À¥À€d`(ÐîàÚJFC–INÛ
þï“ü%åÃEÇC¤PôKò¡l,+!5é!QÄïóz®Z#óBîã\”‹ËN£°ý|÷ƒ¦9•æ|>«êG†d,U
6)rø;Ÿ±î·?¶$ŽšJ§Ú,9”6z«%òýý/Hå^…]¬þ-œ‚/"«KÊõê9£•FÁ{]'jmˆ2rU.²…;;Ù-KàôɹIèß"£°Û¤U»é"í–Jf‰ù?—,ðŸp».³y¨Ëۅ
Zá’dhÉ>kÎÉ­Qiø^k*Ûݲ׏e4ÓßDN  
’d‡RiFô¸¬í‹Ò´Œ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ÛJv0‹fáÁôþ͉lGrÚ|XÍ 
æAh+ÿ™²Ú ¸$Ø»š©ëۜ¾ÆÑüPKA`†5       
í„u÷src/new.xml³±¯ÈÍQ(K-*ÎÌϳU2Ô3PRHÍKÎOÉÌK·U

qÓµP²·ã²)ÊÏ/Ri™EÅ%9©e©9
™)@åJv\
@`SœšœŸ—‚0‚J€%K22‹äŒ•ô¡ºô‘´á0ȤØFa/¦#Lq9Â
‹^}ˆGPKZa†5çSÚÄÐsrc/out.txtsÉO.ÍMÍ+±âå²±¯ÈÍQ(K-*ÎÌϳU2Ô3PRHÍKÎOÉÌK·U

qÓµP²·ª+ÊÏ/Ñi™EÅ%9©e©9
™)@
[EMAIL PROTECTED] °)NMÎÏKAÈÁdÀ²%™EH’ÆJú0}úHq™eVn£°‹SLq:Å«v}¨—tဗË
¤Â¤Â*ÓÖÀjPùٝÁ`
‡fAJ%÷ Ûb„n‘!6‹ ŽEÖg‚®Ï›>3}fh`ˆ%HŒåhPK
ka†5src/PKka†5^ˆa™û{
 "src/Main.javaPKA`†5  í„u÷ 
Hsrc/new.xmlPKZa†5çSÚÄÐ 
æsrc/out.txtPKßÓ
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
dom4j-user mailing list
dom4j-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to