Extract value of attribute node via XPath

How can I extract the value of an attribute node via XPath? A sample XML file is: <parents name=”Parents”> <Parent id=’1′ name=”Parent_1″> <Children name=”Children”> <child name=”Child_2″ id=’2′>child2_Parent_1</child> <child name=”Child_4″ id=’4′>child4_Parent_1</child> <child name=”Child_1″ id=’3′>child1_Parent_1</child> <child name=”Child_3″ id=’1′>child3_Parent_1</child> </Children> </Parent> <Parent id=’2′ name=”Parent_2″> <Children name=”Children”> <child name=”Child_1″ id=’8′>child1_parent2</child> <child name=”Child_2″ id=’7′>child2_parent2</child> <child name=”Child_4″ id=’6′>child4_parent2</child> <child name=”Child_3″ id=’5′>child3_parent2</child> … Read more

XPath contains(text(),’some string’) doesn’t work when used with node with more than one Text subnode

I have a small problem with XPath contains with dom4j … Let’s say my XML is <Home> <Addr> <Street>ABC</Street> <Number>5</Number> <Comment>BLAH BLAH BLAH <br/><br/>ABC</Comment> </Addr> </Home> Let’s say I want to find all the nodes that have ABC in the text given the root Element… So the XPath that I would needed to write would … Read more

Is there a way to get element by XPath using JavaScript in Selenium WebDriver?

I am looking for something like: getElementByXpath(//html[1]/body[1]/div[1]).innerHTML I need to get the innerHTML of elements using JS (to use that in Selenium WebDriver/Java, since WebDriver can’t find it itself), but how? I could use ID attribute, but not all elements have ID attribute. [FIXED] I am using jsoup to get it done in Java. That … Read more

Getting attribute using XPath

Given an XML structure like so: <?xml version=”1.0″ encoding=”ISO-8859-1″?> <bookstore> <book> <title lang=”eng”>Harry Potter</title> <price>29.99</price> </book> <book> <title lang=”eng”>Learning XML</title> <price>39.95</price> </book> </bookstore> How could I get the value of lang (where lang is eng in book title), for the first element? 8 Answers 8

How can I match on an attribute that contains a certain string?

I am having a problem selecting nodes by attribute when the attributes contains more than one word. For example: <div class=”atag btag” /> This is my xpath expression: //*[@class=”atag”] The expression works with <div class=”atag” /> but not for the previous example. How can I select the <div>? 10 Answers 10 Here’s an example that … Read more

Converting JSON to XML in Java

Use the (excellent) JSON-Java library from json.org then JSONObject json = new JSONObject(str); String xml = XML.toString(json); toString can take a second argument to provide the name of the XML root node. This library is also able to convert XML to JSON using XML.toJSONObject(java.lang.String string) Check the Javadoc Link to the the github repository POM <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20160212</version> … Read more