Reputation: 5537
From a name how do I get the sibbling ? I dont wont the nested ones. only the one at the same level. How is this possible?
<root available-locales="en_US" default-locale="en_US">
<dynamic-element instance-id="qk6m4eST" name="image" type="image" index-type="">
<dynamic-element instance-id="fHkE2dfT" name="subimage" type="image" index-type="">
<dynamic-element instance-id="jbthDbC4" name="x" type="text" index-type="">
<dynamic-content>23</dynamic-content>
</dynamic-element>
<dynamic-element instance-id="Rb3qRF6N" name="y" type="text" index-type="">
<dynamic-content>32</dynamic-content>
</dynamic-element>
<dynamic-content><![CDATA[C:\fakepath\images (1).jpeg]]></dynamic-content>
</dynamic-element>
<dynamic-content><![CDATA[C:\fakepath\images (1).jpeg]]></dynamic-content>
</dynamic-element>
<dynamic-element instance-id="MFrJW4OR" name="image" type="image" index-type="">
<dynamic-element instance-id="hjKEtVB6" name="subimage" type="image" index-type="">
<dynamic-element instance-id="NrY9iZ4K" name="x" type="text" index-type="">
<dynamic-content>23</dynamic-content>
</dynamic-element>
<dynamic-element instance-id="7lG7RuAC" name="y" type="text" index-type="">
<dynamic-content>32</dynamic-content>
</dynamic-element>
<dynamic-content><![CDATA[C:\fakepath\grillat.jpeg]]></dynamic-content>
</dynamic-element>
<dynamic-content><![CDATA[C:\fakepath\images (1).jpeg]]></dynamic-content>
</dynamic-element>
</root>
This is my code so far. It prints
test...... 23
Thats the value of first nested child. 23
public List<PopupImage> getImageNodes(Document document) {
List<Node> nodes = document.selectNodes("/root/dynamic-element[@name='" + IMAGE_NODE_NAME + "']");
List<PopupImage> popupImages = new ArrayList<PopupImage>();
for (Node node : nodes) {
// node.selectSingleNode("//following-sibling::dynamic-content[1]").getText();
System.out.println("test...... " + node.selectSingleNode("//following-sibling::dynamic-content").getText());
createPopupImage(node);
popupImages.add(createPopupImage(node));
}
return popupImages;
}
edit: I need every image node and the value of the dynamic value within it.
<root>
<dynamic-element name=image>
<dynamic-element name=subimage/>
<dynamic-content>content1</dynamic-content>
</dynamic-element>
<dynamic-element name=image>
<dynamic-element name=subimage/>
<dynamic-content>content2</dynamic-content>
</dynamic-element>
</root>
edit2:
To clarify more. I need to pair each image node with dynamic content in it.
Upvotes: 2
Views: 905
Reputation: 243459
Use:
/*/dynamic-element[@name='image'][1]/dynamic-content
This selects any dynamic-content
that is a child of the dynamic-element
that is the first dynamic-element
child of the top element, and whose name
attribute's string value is the string "image"
.
And this XPath expression:
/*/dynamic-element[@name='image']/dynamic-content
selects:
<dynamic-content>C:\fakepath\images (1).jpeg</dynamic-content>
<dynamic-content>C:\fakepath\images (1).jpeg</dynamic-content>
If you want just the text nodes, use:
/*/dynamic-element[@name='image']/dynamic-content/text()
This selects the following two text-node children of the elements selected by the previous expression:
C:\fakepath\images (1).jpeg
C:\fakepath\images (1).jpeg
You can get the value of the selected nodes by iterating on the returned XmlNodeList
and obtaining the value of each XmlNode
object contained there.
Upvotes: 1
Reputation: 20710
Currently, after selecting a <dynamic-element>
element, you run the following XPath:
//following-sibling::dynamic-content
Let's look at what this means:
//
: Move back to the document root and consider all nodes, nested at whatever level.following-sibling::
: Find a node that is a sibling of a preceding node (i.e. on the same nesting level and with the same parent node).dynamic-content
: This sibling node must be a <dynamic-content>
element.This is not what you want as described in your text.
Rather, you want to find a following sibling of the current node. That sibling will be a <dynamic-element>
element. In that sibling, you want to find a child node, namely a <dynamic-content>
element.
Hence, try this XPath in your node.selectSingleNode
call:
following-sibling::dynamic-element/dynamic-content
Upvotes: 0