0

Hi! I've spent some time to parse an XML document with XPath. It seeams to be a simple task but I got in troubles since the begining. My code is :

public class QueryXML3 {

    public static void main(String[] args) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder;
        Document doc = null;

        try {
            builder = factory.newDocumentBuilder();
            //doc = builder.parse("SampleExample.xml");
            InputStream is = QueryXML3.class.getClassLoader().getResourceAsStream("SampleXml.xml");
            doc = builder.parse(is);

            XPathFactory xpathFactory = XPathFactory.newInstance();

            // Create XPath object
            XPath xpath = xpathFactory.newXPath();


            Node parNode = getParameterNode(doc, xpath);
            System.out.println("parameter node:" + parNode);

            NodeList res = getParameterNodeList(doc, xpath );
            System.out.println("List of nodes" + res);



        } catch (ParserConfigurationException | SAXException | IOException e) {
            e.printStackTrace();
        }

    }

    public static Node getParameterNode(Document doc, XPath xpath) {
        Node res = null;
        try {
            res = (Node) xpath.evaluate("/definitions/process", doc, XPathConstants.NODE);

        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
        return res;
    }

    public static NodeList getParameterNodeList(Document doc, XPath xpath) {
        NodeList nodeList = null;
        try {
            nodeList = (NodeList) xpath.evaluate("/definitions/process", doc, XPathConstants.NODESET);

            for (int i = 0; i > nodeList.getLength(); i++) {
                System.out.print(nodeList.item(i).getNodeName() + " ");
            }

        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
        return nodeList;
    }
}

As a result i get this:

parameter node:[process: null] List of nodes com.sun.org.apache.xml.internal.dtm.ref.DTMNodeList@2f17aadf

I just want to output all the nodes of my xml file and theire attributes...

Daniel B
  • 8,770
  • 5
  • 43
  • 76
user2966458
  • 181
  • 1
  • 1
  • 5

2 Answers2

0

You are really asking how to serialize an Element to a string - use either a Transformer or DOMImplementationLS.

The NodeList type has no toString() contract and the implementation does not override the default Object.toString(). You need to iterate over the nodes and serialize each Element as above.

Community
  • 1
  • 1
McDowell
  • 107,573
  • 31
  • 204
  • 267
0

You could easily parse an XML file in java using a 3rd party package such as JSoup or JDom.

As an example, here is some simple output of an XML files elements using JSoup:

<note>
   <to>Tove</to>
   <from>Jani</from>
   <heading>Reminder</heading>
   <body>Don't forget me this weekend!</body>
</note>

Java code printing all elements and the selected <from>-element:

String xml = "<note>\n"
        + "<to>Tove</to>\n"
        + "<from>Jani</from>\n"
        + "<heading>Reminder</heading>\n"
        + "<body>Don't forget me this weekend!</body>\n"
        + "</note>";
Document doc = Jsoup.parse(xml, "", Parser.xmlParser());
for (Element e : doc.children()) {
    System.out.println(e);
}

Element fromElement = doc.select("from").first();

System.out.println("\nThis is the <from>-element content:\n" + fromElement);
Daniel B
  • 8,770
  • 5
  • 43
  • 76