gpt4 book ai didi

java - 尝试获取整个 XML 文件。 [#文档: null] error

转载 作者:行者123 更新时间:2023-12-01 09:01:30 24 4
gpt4 key购买 nike

我正在尝试获取整个 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;
}
}

最佳答案

这是因为 doc.toString() 返回带有 [] 的字符串。文档上的 toString() 方法不会将其转换回 XML 格式。要将 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")));
}

然后调用它来输出您的文档:

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

关于java - 尝试获取整个 XML 文件。 [#文档: null] error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41627128/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com