我通过解析内部
HTML页面构建了一个Jsoup文档,
我使用了一个可用的库DOMBuilder但是在解析时我将org.w3c.dom.Document视为null.我无法理解这个问题,尝试搜索但无法找到任何答案. @H_502_4@用于生成W3C DOM文档的代码:
@H_403_14@解决方法
public Document newDocument(String path) throws IOException { Document doc = null; doc = Jsoup.connect(path).timeout(0).get(); return new HtmlDocument<Document>(doc); }@H_502_4@我想将Jsoup文档转换为我的org.w3c.dom.Document
我使用了一个可用的库DOMBuilder但是在解析时我将org.w3c.dom.Document视为null.我无法理解这个问题,尝试搜索但无法找到任何答案. @H_502_4@用于生成W3C DOM文档的代码:
Document jsoupDoc=factory.newDocument("http:localhost/testcases/test_2.html")); org.w3c.dom.Document docu= DOMBuilder.jsoup2DOM(jsoupDoc);@H_502_4@有人可以帮我这个吗?
To retrieve a jsoup document via HTTP,调用Jsoup.connect(…).get().
To load a jsoup document locally,调用Jsoup.parse(新文件(“…”),“UTF-8”).
@H_502_4@对DomBuilder的调用是正确的.
@H_502_4@当你说,
@H_502_4@I used an available library DOMBuilder for this but when parsing I@H_502_4@我认为你的意思是,“我使用了一个可用的库,DOMBuilder,但是在打印结果时,我得到[#document:null].”至少,这是我在尝试打印w3cDoc对象时看到的结果 – 但这并不意味着该对象为null.我能够通过调用getDocumentElement和getChildNodes来遍历文档.
get org.w3c.dom.Document as null.
public static void main(String[] args) { Document jsoupDoc = null; try { jsoupDoc = Jsoup.connect("https://stackoverflow.com/questions/17802445").get(); } catch (IOException e) { e.printStackTrace(); } org.w3c.dom.Document w3cDoc= DOMBuilder.jsoup2DOM(jsoupDoc); Element e = w3cDoc.getDocumentElement(); NodeList childNodes = e.getChildNodes(); Node n = childNodes.item(2); System.out.println(n.getNodeName()); }