本文介绍了哪些图书馆在Google App Engine / Java Servlet上处理XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Eclipse中编写一个Java (要在Google App Engine上托管)并且需要处理XML文档。可以使用哪些可以轻松添加到Eclipse项目的库,并具有良好的示例代码?

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?

推荐答案

我最终使用与SAX API。

I ended up using JAXP with the SAX API.

添加内容像我以下的servlet一样:

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

....

这篇关于哪些图书馆在Google App Engine / Java Servlet上处理XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 18:43