简体   繁体   中英

Find string from xpath which contains tag as child elements In Java

I have following part of XML code and I want to get string from specific tag.

Input XML:

<subject>
   <title> this title has 5 <sup>+</sup> rating star </title>
</subject>

From the above XML I want a string using xpath like

Expected output:

"this title has 5+ rating star"

Note: I have used @XmlPath like

@XmlPath('subject/title/text()')
private String title;

But it returns second value as result in title variable like "rating star"

This is precisely why people advise against using text() unless you have very specialized requirements. The XPath function string() applied to a node returns the string value of the node, which for an element is the concatenation of all the contained text, ignoring markup - which is exactly what you are looking for. So you want string(subject/title) . If you're using a Java API note that the result will now be a string, not a node-set.

Perhaps you could try string(subject/title) . I don't have access to Java for the time being but the string operation is common enough in XPath. It is supposed to concatenate text content without traces of any elements.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM