【发布时间】: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金属
【问题讨论】:
-
你有没有通过尝试下面提到的解决方案获得任何成功?