I have the tag book, then i read out 2 Nodes
CCUSTBLS_KundennummerBLS
CCOMP_MandantFIBU
After this,i have the Nodes "Lieferadresse". Now i want that i have a loop that iterated through the Node "Lieferadresse". My Problem is that i see evertime all childs form all "Lieferadresse" and not only the childs from first "Lieferadresse". I think my Xpath is wrong.
<?xml version="1.0" encoding="utf-8"?>
<eDoc>
<book>
<CCUSTBLS_KundennummerBLS>1143</CCUSTBLS_KundennummerBLS>
<CCOMP_MandantFIBU>1</CCOMP_MandantFIBU>
<Lieferadresse>
<TSHIPBLS_NrLieferadresse>1</TSHIPBLS_NrLieferadresse>
<TCUSTBLS_Kunde>1143</TCUSTBLS_Kunde>
</Lieferadresse>
<Lieferadresse>
<TSHIPBLS_NrLieferadresse>2</TSHIPBLS_NrLieferadresse>
<TCUSTBLS_Kunde>1143</TCUSTBLS_Kunde>
</Lieferadresse>
<Lieferadresse>
<TSHIPBLS_NrLieferadresse>3</TSHIPBLS_NrLieferadresse>
<TCUSTBLS_Kunde>1143</TCUSTBLS_Kunde>
</Lieferadresse>
</book>
</eDoc>
Java Class
List<Node> nodes = document.selectNodes("/eDoc/book/child::*");
List<Node> list = nodes;
for (Node node : list) {
String nodeName = node.getName();
if("Lieferadresse".equals(nodeName)){
List<Node> nodesDelivery = document.selectNodes("//"+nodeName+"/child::*");
System.out.println("------------------------------------------------------");
for(Node nodeDelivery : nodesDelivery){
System.out.println(nodeDelivery.getName() + " " +nodeDelivery.getText());
}
}else{
System.out.println(nodeName + " " +node.getText());
}
}
Result:
Root element :eDoc
CCUSTBLS_KundennummerBLS 1143
CCOMP_MandantFIBU 1
------------------------------------------------------
TSHIPBLS_NrLieferadresse 1
TCUSTBLS_Kunde 1143
TSHIPBLS_NrLieferadresse 2
TCUSTBLS_Kunde 1143
TSHIPBLS_NrLieferadresse 3
TCUSTBLS_Kunde 1143
------------------------------------------------------
------------------------------------------------------
TSHIPBLS_NrLieferadresse 1
TCUSTBLS_Kunde 1143
TSHIPBLS_NrLieferadresse 2
TCUSTBLS_Kunde 1143
TSHIPBLS_NrLieferadresse 3
TCUSTBLS_Kunde 1143
------------------------------------------------------
------------------------------------------------------
TSHIPBLS_NrLieferadresse 1
TCUSTBLS_Kunde 1143
TSHIPBLS_NrLieferadresse 2
TCUSTBLS_Kunde 1143
TSHIPBLS_NrLieferadresse 3
TCUSTBLS_Kunde 1143
------------------------------------------------------
Use a relative path *
and call the selectNodes
method on the node
variable e.g. node.selectNodes("*")
(http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/Node.html#selectNodes(java.lang.String)). If you know you are interested in child elements you can as well access node.elements()
(http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/Element.html#elements()) if you cast to an Element
node first..