XML输出使用JAVA在浏览器中打印

时间:2014-01-20 11:30:22

标签: java xml

在这里,我尝试访问两个第三方API。 我得到了两个xml响应,我将它们合并到一个文件中并将其存储在本地系统中。 如果我在控制台中打印输出,我得到了xml格式的输出,但我想在浏览器中打印它。

我的解决方案不起作用请帮助我。

这是我的代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
  throws  ServletException, IOException {
    response.setContentType("text/xml");
    PrintWriter out=response.getWriter();
    String btn1=request.getParameter("btn1");
    String btn2=request.getParameter("btn2");
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setIgnoringComments(true);
    DocumentBuilder builder = null;
    try {
      builder = domFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e2) {
      e2.printStackTrace();
    }
    Document doc = null;
    try {
      doc = builder.parse(new URL("valid url in my program").openStream());
      response.setContentType("text/xml");
      Object con=doc.getDoctype();
    } catch (SAXException e2) {
      e2.printStackTrace();
    }
    Document doc1 = null;
    try {
      doc1 = builder.parse(new URL(valid url).openStream());
    } catch (SAXException e2) {
      e2.printStackTrace();
    }
    NodeList nodes = doc.getElementsByTagName("events");
    NodeList node1=doc.getElementsByTagName("events");
    Element root=doc.getDocumentElement();
    Element root1 = doc.createElement("ObjectId");
    doc.getDocumentElement().appendChild(root1);
    root.getElementsByTagName("ObjectId").item(0).setTextContent("1");
    node1.item(0).getParentNode().insertBefore(root1,node1.item(0));
    NodeList nodes1 = doc1.getElementsByTagName("events");
    NodeList node2=doc1.getElementsByTagName("event");
    Element root2=doc1.getDocumentElement();
    Element root3= doc1.createElement("ObjectId");
    doc1.getDocumentElement().appendChild(root3);
    root2.getElementsByTagName("ObjectId").item(0).setTextContent("2");
    node2.item(0).getParentNode().insertBefore(root3,node2.item(0));
    for(int i=0;i<nodes1.getLength();i=i+1){
      Node n= (Node) doc.importNode(nodes1.item(i), true);
      nodes.item(i).getParentNode().appendChild(n);
    }
    Transformer transformer = null;
    try {
      transformer = TransformerFactory.newInstance().newTransformer();
    } catch (TransformerConfigurationException e1) {
      e1.printStackTrace();
    } catch (TransformerFactoryConfigurationError e1) {
      e1.printStackTrace();
    }
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(doc);
    try {
      transformer.transform(source,result);
    } catch (TransformerException e) {
      e.printStackTrace();
    }
    Writer output = null;
    output = new BufferedWriter(new FileWriter("merge.xml"));
    String xmlout = result.getWriter().toString();
    output.write(xmlout);
    response.setContentType("text/xml");
    out.write(xmlout);
    //out.println(xmlout);
    //System.out.println(xmlout); 
    //I tried many ways but 
    //it will not print to the browser in xml the format
} 

1 个答案:

答案 0 :(得分:0)

值得注意的;您将ContentType设置三次。它只需要设置一次吗?

你似乎没有回复回复,但我认为你的程序的其他部分正在处理它。

已编辑:此处的其他答案消失了。

需要更多信息来解决出错的问题。请注意,System.out.println()来电不会显示在浏览器中,因为它们是特定于控制台的。