I have just started learning xml parsing through java. I am stuck with the below issue for a quite some time now and need some help.
I have an xml document which look something like this(just the small representation of a big xml document).
`<shipto>
<name>Ola Nordmann</name>
<address>Langgt 23</address>
<city>4000 Stavanger</city>
<country>Norway</country>
</shipto>
<item id="2">
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>
</item>`
I am trying to find payload when attribute 'id="2"'present.
Below is the regex I am using with xpath compile:
XPathExpression expr = xpath.compile("//*[@id=\\"2\\"]");
The issue: This regular expression gives me
`<item id="2">
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>`
When I only need :
<title>Empire Burlesque</title> <note>Special Edition</note> <quantity>1</quantity> <price>10.90</price>
How do I achieve this result?
This question can be really silly as I am just trying to learn xml parsing in java. Please help and thank you in advance.
Use this line to get only the subnodes:
XPathExpression expr = xpath.compile("//*[@id=\"2\"]/*");
Result:
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
This expression returns all children of all <item>
elements with the attribute value @id
= 2
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.