试图获取整个XML文件。 [#document:null]错误

时间:2017-01-13 03:53:16

标签: java xml

我正在尝试在结果上获得整个XMl(如<HTML><BODY><item>a</item>...</HTML>)。但是当我执行此代码时,它会在网页上返回[#document: null]

我搜索了[#document: null]并看到了答案,这不是一个错误。但我看不到任何结果。

我希望看到如下图所示的结果。

我如何才能看到它是否成功进行?

我是Eclipse的新手。对不起基本问题! enter image description here

    package web.component;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.PrintWriter;
    import java.net.URL;
    import java.net.URLConnection;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;

    import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;

    @WebServlet({ "/XmlSelectServlet", "/xmlselect" })
    public class XmlSelectServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //response.setContentType("");
         URL url = new URL("url");
            URLConnection connection = url.openConnection();    

            Document doc;
            try {
                doc = parseXML(connection.getInputStream());
                 response.setCharacterEncoding("UTF-8");
                    PrintWriter out = response.getWriter();
                    out.print(doc.toString());
                    out.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   
    }
     private Document parseXML(InputStream stream) throws Exception{

            DocumentBuilderFactory objDocumentBuilderFactory = null;
            DocumentBuilder objDocumentBuilder = null;
            Document doc = null;

            try{

                objDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
                objDocumentBuilder = objDocumentBuilderFactory.newDocumentBuilder();

                doc = objDocumentBuilder.parse(stream);

            }catch(Exception ex){
                throw ex;
            }       

            return doc;
        }    
}

1 个答案:

答案 0 :(得分:1)

这是因为[]返回带有toString()的字符串。文档上的Document方法不会将其转换回XML格式。要将public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); } 转换回XML,您需要有一些代码来执行此操作:

try {
    doc = parseXML(connection.getInputStream());
    response.setCharacterEncoding("UTF-8");

    // OLD CODE
    // PrintWriter out = response.getWriter();
    // out.print(doc.toString());
    // out.close();

    // NEW CODE
    printDocument(doc, response.getOutputStream());
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

然后调用它来输出你的文件:

npm start