cha*_*uru 9 html-parsing jsoup apache-stanbol
我通过解析内部HTML页面构建了一个Jsoup文档,
public Document newDocument(String path) throws IOException {
Document doc = null;
doc = Jsoup.connect(path).timeout(0).get();
return new HtmlDocument<Document>(doc);
}
Run Code Online (Sandbox Code Playgroud)
我想将Jsoup文档转换为org.w3c.dom.Document
我使用的可用库DOMBuilder,但是在解析时我得到org.w3c.dom.Documentnull.我无法理解这个问题,尝试搜索但无法找到任何答案.
用于生成W3C DOM文档的代码:
Document jsoupDoc=factory.newDocument("http:localhost/testcases/test_2.html"));
org.w3c.dom.Document docu= DOMBuilder.jsoup2DOM(jsoupDoc);
Run Code Online (Sandbox Code Playgroud)
有人可以帮我这个吗?
Ste*_*han 19
或者,Jsoup为W3CDom类提供方法fromJsoup.此方法将Jsoup文档转换为W3C文档.
Document jsoupDoc = ...
W3CDom w3cDom = new W3CDom();
org.w3c.dom.Document w3cDoc = w3cDom.fromJsoup(jsoupDoc);
Run Code Online (Sandbox Code Playgroud)
更新:
要通过HTTP检索jsoup文档,请拨打电话Jsoup.connect(...).get(). 要在本地加载jsoup文档,请拨打电话Jsoup.parse(new File("..."), "UTF-8").
呼叫DomBuilder是正确的.
当你说,
我使用了一个可用的DOMBuilder库,但在解析时我将org.w3c.dom.Document视为null.
我想你的意思是,"我使用了一个可用的库,DOMBuilder,但是在打印结果时,我得到了[#document: null]." 至少,这是我在尝试打印w3cDoc对象时看到的结果- 但这并不意味着对象为空.我能够通过调用getDocumentElement和遍历文档getChildNodes.
public static void main(String[] args) {
Document jsoupDoc = null;
try {
jsoupDoc = Jsoup.connect("http://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());
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7141 次 |
| 最近记录: |