简体   繁体   English

Java DOM解析器返回空文档

[英]Java DOM parser returns null document

I have an HTML template which I want to read in: 我有一个要阅读的HTML模板:

<html>
   <head>
      <title>TEST</title>
   </head>
   <body>
      <h1 id="hey">Hello, World!</h1>
   </body>
</html>

I want find the tag with the id hey and then paste in new stuff (eg new tags). 我想找到id为hey的标签,然后粘贴新内容(例如新标签)。 For this purpose I use the DOM parser. 为此,我使用DOM解析器。 But my code returns me null : 但是我的代码返回了null

public static void main(String[] args) {

    try {
        File file = new File("C:\\Users\\<username>\\Desktop\\template.html");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(file);
        doc.getDocumentElement().normalize();

        System.out.println(doc.getElementById("hey")); // returns null

    } catch (Exception e) {
        e.printStackTrace();
    }

}

What am I doing wrong? 我究竟做错了什么?

You are trying to parse a piece of XML with the Java XML API, that is very compliant with the XML specification and doesn't help the casual developer. 您正在尝试使用Java XML API解析一段XML,该XML非常符合XML规范并且对临时开发人员没有帮助。

In XML an attribute named id is not automatically of ID type, and thus the XML implementation doesn't get it with .getElementById() . 在XML中,名为id的属性不会自动具有ID类型,因此XML实现无法通过.getElementById()获得它。 Either you use another library (Jsoup for example), or instruct the parser to treat id as an ID (via the DTD) or you use custom code. 您可以使用另一个库(例如,Jsoup),或者指示解析器将id视为ID(通过DTD),或者使用自定义代码。

I modified your example to using jsoup 我将您的示例修改为使用jsoup

public static void main(String[] args) {
        try {
            File file = new File("C:\\Users\\<username>\\Desktop\\template.html");
            Document doc = Jsoup.parse(file, "UTF8");          
            Element elementById = doc.getElementById("hey");
            System.out.println("hey ="+doc.getElementById("hey").ownText());
            System.out.println("hey ="+doc.getElementById("hey"));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM