Michael A
Michael A

Reputation: 5840

Xml parsing includes inline parsing

Using:

 import org.w3c.dom.*;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.DocumentBuilder;

how can i extract xml data? for example for this xml file:

<request method="POST" url="/devices/test/planner" body="*">
  <response statusCode="200">
    <header>
      <headerParameters>
        <headerParameter name="Content-Type">Content-Type=application/xml</headerParameter>
      </headerParameters>
    </header>
    <body>booking created!</body>
  </response>
</request>

how do i extract the data simply? Also how can i extract data from inline row?

<request method="POST" url="/devices/test/planner" body="*">

Thanks?

Upvotes: 0

Views: 400

Answers (2)

Wayne
Wayne

Reputation: 60424

I second the recommendation to use XPath. You could manually traverse the DOM, but why? XPath was designed to solve this problem. Here's a complete Java/JAXP/XPath example (sans error-checking and exception handling):

import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Xpather {
    public static void main(String[] args) 
            throws XPathExpressionException, ParserConfigurationException, 
            SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new InputSource("workbook.xml"));
        XPath xpath = XPathFactory.newInstance().newXPath();
        Node body = (Node) xpath.evaluate("/request/response/body", doc,
                XPathConstants.NODE);
        System.out.println(body.getTextContent());
        Node url = (Node) xpath.evaluate("/request/@url", doc,
                XPathConstants.NODE);
        System.out.println(url.getNodeValue());
    }
}

This code prints the contents of the body element and the value of the url attribute on the request element.

Output:

booking created!

/devices/test/planner

Upvotes: 2

ziesemer
ziesemer

Reputation: 28697

Use XPath - also readily available through standard Java APIs. Once you have your Document, Java's XPath can evaluate directly off of it - so having your DocumentBuilder and such will still be used.

For example, you can obtain just the <request/> element, using /request, or the request's URL, using something like /request/@url.

An alternative - especially for high-performance usage scenarios where you may be processing many documents following the same schema - is to use SAX parsing instead, where you will receive an event for every XML element processed, and given its name and list of attributes.

Upvotes: 1

Related Questions