【发布时间】:2016-05-27 20:45:59
【问题描述】:
我有一个要阅读的 HTML 模板:
<html>
<head>
<title>TEST</title>
</head>
<body>
<h1 id="hey">Hello, World!</h1>
</body>
</html>
我想找到 ID 为 hey 的标签,然后粘贴新内容(例如新标签)。为此,我使用 DOM 解析器。但是我的代码返回给我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();
}
}
我做错了什么?
【问题讨论】:
标签: java dom null document domparser