I am writing a java program to search for text and return its node information.
我正在编写一个java程序来搜索文本并返回其节点信息。
- The program should search for text anywhere in the document and.
- Return 5 predefined node and node-values.
该程序应在文档中的任何位置搜索文本。
返回5个预定义节点和节点值。
Sample xml file : -
示例xml文件: -
<project xmlns="https://example.hoom/go/bing">
<name>purchaseOrder </name>
<property name="included" type="hidden">true</property>
<locales>
<locale>en</locale>
<locale>hi</locale>
</locales>
<defaultLocale>en</defaultLocale>
<gamespace>
<name locale="en">hambook</name>
<name locale="de">hambook</name>
<lastChanged>2014-03-05T18:47:30</lastChanged>
<lastChangedBy>userx</lastChangedBy>
<property name="included" type="hidden">true</property>
<gamespace>
<name locale="en">DbBook</name>
<name locale="zw">DbBook</name>
<hecrotSubject status="valid">
<name locale="en">hexValue</name>
<name locale="zw">hexValue</name>
<hecrotItem>
<name locale="en">hireValue</name>
<name locale="zw">hireValue</name>
<hello>searchTerm</hello>
</hecrotItem>
</hecrotSubject>
</gamespace>
</gamespace>
<gamespace>
<name locale="en">Names</name>
<lastChanged>2016-01-12T12:42:46</lastChanged>
<gamespace>
<name locale="en">Database Layer</name>
<name locale="zw">Database Layer</name>
<hecrotSubject status="valid">
<name locale="en">qsxyz</name>
<hecrotItem>
<name locale="en">myName</name>
<hello>...Hi there..</hello>
</hecrotItem>
</hecrotSubject>
</gamespace>
</gamespace>
</project>
My current xpath is :-
我目前的xpath是: -
"//*[local-name()='gamespace']/*[local-name()='hecrotSubject']/*[local-name()='hecrotItem'][contains(., '"& searchTerm &"')]/ancestor-or-self::*/*[local-name()='name' and @locale='en']"
Which is giving only the root tag using xpath.compile().evaluate()
. While result I need is
哪个只使用xpath.compile()。evaluate()来提供根标记。虽然我需要的结果是
name value of five predefined nodes if they contain the search text (searchTerm in this sample xml).
如果它们包含搜索文本(此示例xml中的searchTerm),则为五个预定义节点的名称值。
sample result should be:-
样本结果应该是: -
Project - purchaseOrder
项目 - purchaseOrder
gamespace - hambook
游戏空间 - hambook
gamespace - DbBook
游戏空间 - DbBook
hecrotSubject - hexValue
hecrotSubject - hexValue
hecrotItem - hireValue
hecrotItem - hireValue
Edit
I am using following statements in java : -
我在java中使用以下语句: -
String expression = Xpath;
Strings vals = xPath.compile(expression).evaluate(xmlDocument);
System.out.println(vals);
1 个解决方案
#1
0
I do not know why you get only the root tag.
But in your xpaht the case of some values (node names) are wrong. Fixing hecrotSubject and hecrotItem and it should work.
But by the way I do not see why you are using local-name with setting up the right name-space something like the following should also do:
我不知道为什么你只得到根标签。但是在你的xpaht中,某些值(节点名称)的情况是错误的。修复hecrotSubject和hecrotItem,它应该工作。但顺便说一句,我不明白你为什么使用local-name设置正确的名称空间,如下所示:
/b:gamespace/b:hecrotsubject/b:hecrotitem[contains(., 'searchTerm')]/ancestor-or-self::*/b:name[@locale='en']"
Update doe to updated question
更新doe以更新问题
The main problem is that you need iterate over the NodeList
which could be returned by xpath evaluate
.
主要问题是你需要遍历NodeList,它可以由xpath evaluate返回。
Have a look to e.g.: How to read XML using XPath in Java
看看例如:如何在Java中使用XPath读取XML
#1
0
I do not know why you get only the root tag.
But in your xpaht the case of some values (node names) are wrong. Fixing hecrotSubject and hecrotItem and it should work.
But by the way I do not see why you are using local-name with setting up the right name-space something like the following should also do:
我不知道为什么你只得到根标签。但是在你的xpaht中,某些值(节点名称)的情况是错误的。修复hecrotSubject和hecrotItem,它应该工作。但顺便说一句,我不明白你为什么使用local-name设置正确的名称空间,如下所示:
/b:gamespace/b:hecrotsubject/b:hecrotitem[contains(., 'searchTerm')]/ancestor-or-self::*/b:name[@locale='en']"
Update doe to updated question
更新doe以更新问题
The main problem is that you need iterate over the NodeList
which could be returned by xpath evaluate
.
主要问题是你需要遍历NodeList,它可以由xpath evaluate返回。
Have a look to e.g.: How to read XML using XPath in Java
看看例如:如何在Java中使用XPath读取XML