Java中使用Xpath进行XML解析以获取所有值匹配的元素
我有一个没有特定结构的动态 xml 文件。例如;
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:ietf:params:xml:ns:yang:yin:1" name="ui-leaf-type-second">
<yang-version value="1.1"/>
<namespace uri="ui-leaf-type-second"/>
<prefix value="uil-type-second"/>
<description>
<text>This is basic yang types example</text>
</description>
<revision date="2022-02-02">
<description>
<text>initial revision</text>
</description>
</revision>
<typedef name="laser-range">
<type name="uint8">
<range value="0 .. 100"/>
</type>
<description>
<text>Percentage</text>
</description>
</typedef>
<grouping name="grp-range">
<leaf name="name-of-range">
<type name="boolean"/>
</leaf>
</grouping>
<container name="cnt-range">
<uses name="grp-range"/>
</container>
</module>
我想获得具有我搜索价值的完整元素。 例如,我想获取一个具有 @name = "laser-range" 的元素,
它应该返回整个元素:
<typedef name="laser-range">
<type name="uint8">
<range value="0 .. 100"/>
</type>
<description>
<text>Percentage</text>
</description>
</typedef>
如果我搜索 @name='name-of-range'< /strong>,它应该返回
<leaf name="name-of-range">
<type name="boolean"/>
</leaf>
我尝试使用 xpath 获取那些,但它只返回标记中的值。
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder;
Document doc = null;
try {
builder = factory.newDocumentBuilder();
doc = builder.parse(tmpFile.toFile());
XPath xpath = XPathFactory.newInstance().newXPath();
// XPath Query for showing all nodes value
XPathExpression expr = xpath.compile("//*[@name='laser-range']");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
//result includes only "Percentage"
}
我还尝试使用 xpath 的 @* 表示法,但它也无法给我一个解决方案。
那么,我怎样才能做到呢?有人可以告诉我一种方法吗?
I have a dynamic xml file that has not specific structure. For example;
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:ietf:params:xml:ns:yang:yin:1" name="ui-leaf-type-second">
<yang-version value="1.1"/>
<namespace uri="ui-leaf-type-second"/>
<prefix value="uil-type-second"/>
<description>
<text>This is basic yang types example</text>
</description>
<revision date="2022-02-02">
<description>
<text>initial revision</text>
</description>
</revision>
<typedef name="laser-range">
<type name="uint8">
<range value="0 .. 100"/>
</type>
<description>
<text>Percentage</text>
</description>
</typedef>
<grouping name="grp-range">
<leaf name="name-of-range">
<type name="boolean"/>
</leaf>
</grouping>
<container name="cnt-range">
<uses name="grp-range"/>
</container>
</module>
I want to get full element which has value that I searching for.
For example, I want to get an element that have @name = "laser-range"
It should return whole element:
<typedef name="laser-range">
<type name="uint8">
<range value="0 .. 100"/>
</type>
<description>
<text>Percentage</text>
</description>
</typedef>
If I search for @name='name-of-range', it should return
<leaf name="name-of-range">
<type name="boolean"/>
</leaf>
I tried to get those with xpath but it is returning only values in the tag.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder;
Document doc = null;
try {
builder = factory.newDocumentBuilder();
doc = builder.parse(tmpFile.toFile());
XPath xpath = XPathFactory.newInstance().newXPath();
// XPath Query for showing all nodes value
XPathExpression expr = xpath.compile("//*[@name='laser-range']");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
//result includes only "Percentage"
}
I also tried to use @* notation of xpath but it could not give me a solution too.
So, how can I make it? Can someone to show me a way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论