【问题标题】:What libraries are there for processing XML on Google App Engine/Java Servlet有哪些库可以在 Google App Engine/Java Servlet 上处理 XML
【发布时间】:2010-11-05 04:28:20
【问题描述】:

我正在 Eclipse 中编写 Java servlet(将托管在 Google App Engine 上)并且需要处理 XML 文档。哪些库易于添加到 Eclipse 项目并具有良好的示例代码?

【问题讨论】:

    标签: java xml eclipse servlets


    【解决方案1】:

    我最终将JAXP 与 SAX API 一起使用。

    在我的 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();
    
    ....
    

    【讨论】:

      【解决方案2】:

      Xerces(提供 SAX 和 DOM 实现)和 Xalan(提供对转换的支持) - 自 1.5 以来都与 JDK 捆绑在一起,因此已经在标准 Java 安装中进行了配置

      【讨论】:

      • App Engine 提供的 JVM 不是标准的 Java 安装。你知道它们是否可用吗?
      • 嗨,Sam - 我没有使用过 Google App Engine,但对文档的快速浏览表明它只是具有一些安全限制的 Java 6。看看这里 - code.google.com/appengine/docs/whatisgoogleappengine.html
      • AppEngine 目前不支持 Xerces。安全问题。
      【解决方案3】:

      取决于我想你的目标是什么。我使用 JAXB 将 xml 编组/解组到 Java 对象,它相当快速、易于扩展并且具有良好的社区支持。

      如果您不想开始编写模式,那么我很幸运 dom4j,它的学习曲线更短。

      【讨论】:

      • 呸,我想处理 XML;我不想自己写任何东西,所以 JAXB 出局了。
      • 你也可以使用注解来标记你想要绑定的对象。我个人没有使用过这种方法,但它可以让您不必编写模式。我承认 JAXB 对于很多应用程序来说很容易被矫枉过正。
      【解决方案4】:

      您可以使用需要 xerces SAXParser 的 JDOM。但是,AppEngine 不提供 xerces 库。您可以通过将其复制到项目的 WEB-INF/lib 文件夹中来添加它。

      【讨论】:

        【解决方案5】:
        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;
            }
        

        【讨论】:

          【解决方案6】:

          JDom 具有比标准 Java XML api 更好(更简单)的接口。

          【讨论】:

            【解决方案7】:

            您可以使用在非 servlet 环境中使用的完全相同的库。

            【讨论】:

              【解决方案8】:

              另一个比 Xerces 速度更快的选择(我上次比较它们时)是Saxon

              【讨论】:

                猜你喜欢
                • 2010-11-24
                • 1970-01-01
                • 2015-03-05
                • 2016-03-04
                • 1970-01-01
                • 2019-12-06
                • 1970-01-01
                • 2014-12-11
                • 2010-12-20
                相关资源
                最近更新 更多