【问题标题】:Get Child element in XML from XPath java从 XPath java 中获取 XML 中的子元素
【发布时间】:2018-02-07 09:29:34
【问题描述】:

我有一个如下所示的 XML:

<element type="PRICE_SPECIFIC_SIMPLE_QUERY">
    <query>
        <and>
            <match field="product" value="Bottle"/>
            <not>
                <match field="status" value="Cancel"/>
            </not>
            <not>
                <match field="material">
                    <value>GLASS</value>
                    <value>METAL</value>
                </match>
            </not>
            <greaterThan field="LastDateToPrice" value="2017-12-20"/>
            <match field="company" value="MILTON"/>
        </and>
    </query>
</element>

使用 XPath = /element[@type='PRICE_SPECIFIC_SIMPLE_QUERY',我想要内部 XML 作为字符串...如下所示:

<query>
    <and>
        <match field="product" value="Bottle"/>
        <not>
            <match field="status" value="Cancel"/>
        </not>
        <not>
            <match field="material">
                <value>GLASS</value>
                <value>METAL</value>
            </match>
        </not>
        <greaterThan field="LastDateToPrice" value="2017-12-20"/>
        <match field="company" value="MILTON"/>
    </and>
</query>

但是通过在线提供的示例,我得到了内部值,而不是 XML。我的示例代码如下所示:

    XPathFactory xpathFactory = XPathFactory.newInstance();

    XPath xpath = xpathFactory.newXPath();
    String queryType = "ICE_SPECIFIC_SIMPLE_QUERY";

    XPathExpression expr = xpath.compile("/element[@type='" + queryType + "']");
    String innerElement = (String) expr.evaluate(doc, XPathConstants.STRING);

实际输出:玻璃\n金属

【问题讨论】:

  • 你有没有通过尝试下面提到的解决方案获得任何成功?

标签: java xml xpath


【解决方案1】:

基本上,您需要先获取节点(完整的 XML 片段),然后才将其转换为字符串,在此 answer 中使用 nodeToString 之类的函数。

【讨论】:

    【解决方案2】:

    我假设您正在寻找的是innerHTML

    这可能会有所帮助:

    String source = driver.findElement(By.xpath("/element[@type='PRICE_SPECIFIC_SIMPLE_QUERY'")).getAttribute("innerHTML");

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-12
      • 1970-01-01
      • 2018-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多