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

后端 未结 8 1146
梦谈多话
梦谈多话 2021-01-18 14:23

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 Eclip

8条回答
  •  北恋
    北恋 (楼主)
    2021-01-18 15:09

    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=");
            }
        }
    
        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;
        }
    

提交回复
热议问题