【问题标题】:VTD-XML: How to get XPath of current element?VTD-XML:如何获取当前元素的 XPath?
【发布时间】:2015-08-01 08:28:11
【问题描述】:

我正在使用 VTD 解析和拆分非常大的 XML 文件,并在调用 AutoPilot.evalXPath() 后需要光标指向的当前元素的实际 XPath。

考虑以下 XML:

<atlas>
    <continents>
        <continent>
            <name>North America</name>
            <countries>
                <country>
                    <name>Canada</name>
                </country>
                <country>
                    <name>US</name>
                </country>
            </countries>
        </continent>
        <continent>
            <name>South America</name>
            <countries>
                <country>
                    <name>Chile</name>
                </country>
                <country>
                    <name>Brazil</name>
                </country>
            </countries>
        </continent>
    </continents>
</atlas>

我正在用autopilot.selectXPath("/atlas/continents/continent/countries/country");初始化AutoPilot

当我使用 autopilot.evalXPath() 遍历匹配项时,我需要每个元素的绝对 XPath,例如 /atlast/continents/continent[1]/countries/country[2]

我尝试了VTDNavAutoPilot中的几乎所有方法-

  • VTDNav.getXPathStringVal() / VTDNav.getXPathStringVal(short mode)
  • VTDNav.toNormalizedXPathString(int j)
  • AutoPilot.getExprString()

有什么想法吗?

【问题讨论】:

    标签: xml xpath vtd-xml


    【解决方案1】:

    我不认为 vtd 在当前版本中提供功能,实际上我不知道有任何提供此功能的 api。我认为原因是任何当前元素节点都有许多不同的可能 XPath ......你怎么看?

    【讨论】:

    • 感谢您的回复@vtd-xml-author。是的,有许多不同的 XPath 可以到达一个节点。而且这种能力在 JDOM 等其他解析器中似乎也不存在。不确定我是否在这里遗漏了一些东西,但是,不是每个节点都有自己独特的绝对 XPath 吗?在上面的示例中,Canada 可以使用多个 XPath 来访问,但绝对 XPath /atlast/continents/continent[0]/countries/country[0]Canada 所独有的。看起来很简单,但我敢肯定,解析器背后发生的事情比我想象的要多得多:)
    • 怎么样/atlast/continents/continent[0]/countries/country[0]/../country[0]?
    猜你喜欢
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    • 2014-08-09
    • 1970-01-01
    相关资源
    最近更新 更多