简体   繁体   English

从xml查找所有有效的xpath

[英]Finding all valid xpath from xml

I am trying to write a program in java where in i can find all the xpath for the given xml.I found out the link on the internet xpath generator but it does not work when one element can repeat multipletimes for example if we have xml like the following :- 我正在尝试用Java编写一个程序,在其中我可以找到给定xml的所有xpath。我在互联网xpath生成器上找到了链接,但是当一个元素可以重复多次时(例如,如果我们有xml下列 :-

<?xml version="1.0" encoding="UTF-8"?>
<Report>
    <Name>
        <FirstName>A</FirstName>
        <LastName>B</LastName>
        <MiddleName>C</MiddleName>

    </Name>
    <Name>
        <FirstName>D</FirstName>
        <LastName>E</LastName>
        <MiddleName>S</MiddleName>
    </Name>
</Report>

It will produce xpaths :- /Report/Name/firstname for both firstname nodes. 它将为两个名节点生成xpaths:-/ Report / Name / firstname。 but the expected should be /Report/Name 1 /firstname and /Report/Name[2]/firstname 但应为/ Report / Name 1 / first和/ Report / Name [2] / firstname

Any ideas? 有任何想法吗?

I think you may have to do this yourself. 我认为您可能必须自己做。

Using a SAX parser will make it straightforward. 使用SAX解析器将使其简单明了。 Just maintain a stack of the elements you encounter and a count so you can increment the indexes ( /Report/Name[1] , /Report/Name[2] ) easily. 只需维护您遇到的元素和计数的堆栈,即可轻松增加索引( /Report/Name[1]/Report/Name[2] )。

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

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