简体   繁体   English

在Java中从xpath查找包含标签作为子元素的字符串

[英]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. 我有以下XML代码的一部分,我想从特定标记中获取字符串。

Input XML: 输入XML:

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

From the above XML I want a string using xpath like 从上面的XML,我想要一个使用xpath的字符串,例如

Expected output: 预期产量:

"this title has 5+ rating star"

Note: I have used @XmlPath like 注意:我使用过@XmlPath之类的

@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. 这就是为什么除非您有非常特殊的要求,否则人们建议不要使用text()的原因。 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. 应用于节点的XPath函数string()返回该节点的字符串值,对于元素来说,该字符串值是所有包含的文本的串联,而忽略标记-这正是您要查找的内容。 So you want string(subject/title) . 所以你想要string(subject/title) If you're using a Java API note that the result will now be a string, not a node-set. 如果您使用的是Java API,请注意,现在的结果将是字符串,而不是节点集。

Perhaps you could try string(subject/title) . 也许您可以尝试使用string(subject/title) I don't have access to Java for the time being but the string operation is common enough in XPath. 我暂时无法访问Java,但是字符串操作在XPath中足够普遍。 It is supposed to concatenate text content without traces of any elements. 它应该连接文本内容而没有任何元素的痕迹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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