[英]Get Child element in XML from XPath java
我有如下所示的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作為String ...如下所示:
<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金屬
基本上,您需要首先獲取節點(完整的XML片段),然后再使用此答案中的諸如nodeToString
函數將其轉換為String。
我假設您正在尋找的是innerHTML
。
這可能會有所幫助:
String source = driver.findElement(By.xpath("/element[@type='PRICE_SPECIFIC_SIMPLE_QUERY'")).getAttribute("innerHTML");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.