Home » Java » What libraries are there for processing XML on Google App Engine/Java Servlet

What libraries are there for processing XML on Google App Engine/Java Servlet

Posted by: admin December 28, 2021 Leave a comment

Questions:

I’m writing a Java servlet in Eclipse (to be hosted on Google App Engine) and need to process an XML document. What libraries are available that are easy to add to an Eclipse project and have good example code?

Answers:

I ended up using JAXP with the SAX API.

Adding something like the following to my servlet:

import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;

....

InputStream in = connection.getInputStream();

InputSource responseXML = new InputSource(in);
final StringBuilder response = new StringBuilder();
DefaultHandler myHandler = new DefaultHandler() {

    public void startElement(String uri, String localName, String qName, 
            Attributes attributes) throws SAXException {
        if (localName.equals("elementname")) {
            response.append(attributes.getValue("attributename"));
            inElement = true;
        }
    }
    public void characters(char [] buf, int offset, int len) {
        if (inElement) {
            inElement = false;
            String s = new String(buf, offset, len);
            response.append(s);
            response.append("\n");
        }
    }
};

SAXParserFactory factory = SAXParserFactory.newInstance();
try {
    SAXParser parser = factory.newSAXParser();
    parser.parse(responseXML, myHandler);
} catch (ParserConfigurationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (SAXException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

in.close();
connection.disconnect();

....

###

Xerces (that provides both SAX and DOM implementations) and Xalan (that provides support for transformations) – both have been bundled with the JDK since 1.5 and are therefore already configured in a standard Java install

###

Depends what your goals are I suppose. I’ve used JAXB for marshalling/unmarshalling xml to Java objects and it’s fairly quick, easily extensible and has good community support.

If you don’t want to get into writing schemas and what not then I’ve had good luck with dom4j and it has a smaller learning curve.

###

You can use JDOM which requires xerces SAXParser. However, AppEngine does not provide the xerces library. You can add it by copying it in the WEB-INF/lib fold of your project.

###

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;


    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        String content = req.getParameter("content");
        Document doc = parseXml(content);
        resp.setContentType("text/plain");
        if (doc != null)
        {
            resp.getWriter().println(doc.getDocumentElement().getNodeName());
        }
        else
        {
            resp.getWriter().println("no input/bad xml input. please send parameter content=<xml>");
        }
    }

    private static Document parseXml(String strXml)
    {
        Document doc = null;
        String strError;
        try
        {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();

            StringReader reader = new StringReader( strXml );
            InputSource inputSource = new InputSource( reader );

            doc = db.parse(inputSource);

            return doc;
        }
        catch (IOException ioe)
        {
            strError = ioe.toString();
        }
        catch (ParserConfigurationException pce)
        {
            strError = pce.toString();
        }
        catch (SAXException se)
        {
            strError = se.toString();
        }
        catch (Exception e)
        {
            strError = e.toString();
        }

        log.severe("parseXml: " + strError);
        return null;
    }

###

JDom has a better (simpler) interface than the standard Java XML apis.

###

you can use exactly the same libraries you use in a non-servlet environment.

###

Another choice, which has better speed than Xerces (the last time I compared them) was Saxon.