4

So I am pulling out specific tags and elements from an xml document using xPath and storing it in an object. My question is how can I convert this object to a string. I have tried the .toString() method, but all that it give me is the following:

LocalHost test: org.apache.xml.dtm.ref.DTMNodeList@717e717e

my code is below:

DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            domFactory.setNamespaceAware(true); // never forget this!
            DocumentBuilder builder = domFactory.newDocumentBuilder();
            org.w3c.dom.Document doc =  builder.parse("src/webSphere/testcases/positiveCode-data.xml");

            XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();
            XPathExpression expr 
             = xpath.compile(" /tests/class/method/params[@name='config']/param/text()");
            MQQueueConnectionFactory cf = new MQQueueConnectionFactory();
            Object result = expr.evaluate(doc, XPathConstants.NODESET);


            System.out.println("LocalHost test: " + result.toString());

here is my xml file:

<?xml version ="1.0" encoding = "UTF-8"?>
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://jtestcase.sourceforge.net/dtd/jtestcase2.xsd">
    <class name="Success">
        <method name="success">
            <test-case name="positive-code">
                <params name="config">
                    <param name="hostName" type="java.lang.String">localhost</param>
                    <param name="port" type="int">7080</param>
                    <param name="transportType" type="java.lang.String">10</param>
                    <param name="queueManager" type="java.lang.String">MB8QMGR</param>
                    <param name="channel" type="java.lang.String">10</param>
                    <param name="inputQueue" type="java.lang.String">10</param>
                    <param name="outputQueue" type="java.lang.String">20</param>
                    <param name="testFile" type="java.lang.String">+</param>
                    <param name="expectedResultFile" type="java.lang.String">+</param>
                </params>
                <asserts>
                    <assert name="result" type="int" action="EQUALS">
                        30
                </assert>
                </asserts>
            </test-case>


        </method>
    </class>
</tests>
3
  • You need to override the toString method of the Object you want to get printed in Readable way, otherwise you will get the Object reference value using toString Commented Jul 24, 2013 at 8:25
  • And how do I do that? Commented Jul 24, 2013 at 8:26
  • It seems that DTMNodeList does not override Objects toString(). So you have to handle the Type DTMNodeList explicitly. Are you sure it's always this type? Commented Jul 24, 2013 at 8:31

1 Answer 1

2

Sure. You can see that you have an instance of a NodeList.

You can get the length of the list and then all Node items at a specific indexes and print them out based on what you expect of them - most likely their text content, so Node#getTextContent() (or anything other you want to print out - node name, node type etc.) in a loop.

NodeList resultList = (NodeList)expr.evaluate(doc, XPathConstants.NODESET);
int listLength = resultList.getLength();
for (int i = 0; i < listLength; i++) {
    System.out.println("LocalHost test (" + i + "): " + resultList.item(i).getTextContent());
}

EDIT

If you expect a single String value simply do

String resultList = (String)expr.evaluate(doc, XPathConstants.STRING);

This will convert the result to a String following the XPath String conversion rules.

Sign up to request clarification or add additional context in comments.

5 Comments

Its not resolving DTMNNodeList as a type, do I have to import this or add a jar somewhere?
It is. You specified that it will return a NodeList when you executed expr.evaluate(doc, XPathConstants.NODESET). If you expected a single String and not a list of text nodes, you might have wanted to use expr.evaluate(doc, XPathConstants.STRING). Anyway, you will have to manually write a cast there (which will be safe).
I just seem to get a null or blank. im using the following line just to see if Im getting one: String result = (String) expr.evaluate(doc, XPathConstants.STRING); but with no luck. What am I doing wrong?
@AndrewMurphy Your XPath expresion is wrong. You forgot to add a location step for <test-case name="positive-code">. Anyway, you will get a NodeList of String values, so I recommend the first approach from my solution.
yea just saw that now. Such an idiot and not enough coffee. Thank you very much Slanec, a gentleman and a scholar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.