加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

html-parsing – 如何将Jsoup文档转换为W3C文档?

发布时间:2020-12-14 18:43:04 所属栏目:资源 来源:网络整理
导读:我通过解析内部 HTML页面构建了一个Jsoup文档, public Document newDocument(String path) throws IOException { Document doc = null; doc = Jsoup.connect(path).timeout(0).get(); return new HtmlDocumentDocument(doc);} 我想将Jsoup文档转换为我的org.
我通过解析内部 HTML页面构建了一个Jsoup文档,
public Document newDocument(String path) throws IOException {

    Document doc = null;
    doc = Jsoup.connect(path).timeout(0).get();
            return new HtmlDocument<Document>(doc);
}

我想将Jsoup文档转换为我的org.w3c.dom.Document
我使用了一个可用的库DOMBuilder但是在解析时我将org.w3c.dom.Document视为null.我无法理解这个问题,尝试搜索但无法找到任何答案.

用于生成W3C DOM文档的代码:

Document jsoupDoc=factory.newDocument("http:localhost/testcases/test_2.html"));
org.w3c.dom.Document docu= DOMBuilder.jsoup2DOM(jsoupDoc);

有人可以帮我这个吗?

解决方法

To retrieve a jsoup document via HTTP,调用Jsoup.connect(…).get(). To load a jsoup document locally,调用Jsoup.parse(新文件(“…”),“UTF-8”).

对DomBuilder的调用是正确的.

当你说,

I used an available library DOMBuilder for this but when parsing I
get org.w3c.dom.Document as null.

我认为你的意思是,“我使用了一个可用的库,DOMBuilder,但是在打印结果时,我得到[#document:null].”至少,这是我在尝试打印w3cDoc对象时看到的结果 – 但这并不意味着该对象为null.我能够通过调用getDocumentElement和getChildNodes来遍历文档.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读