0

I have an XML shown below:

<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>

With XPath = /element[@type='PRICE_SPECIFIC_SIMPLE_QUERY', I want inner XML as String...as shown below:

<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>

But with examples available online, I am getting inner values, but not XML. My sample code is shown below:

    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);

Actual Output : GLASS \n METAL

pankaj_ar
  • 757
  • 2
  • 10
  • 33

2 Answers2

0

Basically, you need to get first the node (full XML fragment), and only afterwards convert it to String, using some function like nodeToString in this answer.

Edvins
  • 406
  • 5
  • 9
0

I assume what you are looking for is innerHTML.

This might help:

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

hiren
  • 1,067
  • 1
  • 9
  • 16