DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(new File("mypage.html"));
document.getDocumentElement().normalize();
NodeList nodeList = document.getElementsByTagName("input");
for(int i=0 , size = nodeList.getLength() ; i < size ; i++){
System.out.println(nodeList.item(i).getAttributes().getNamedItem("name").getNodeValue());
}
我使用上述代码从某些html文件中获取输入标记的名称属性中的值。在从html文件读取名称属性值时出错
为了更清楚我想要的打印 - 名字和姓氏从这个简单的HTML文件代码
<input type="text" name="firstname" value="Mickey" />
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse" />
但是,错误出现: [致命错误] ...元素类型“输入”必须由相匹配的结束标记“/输入>终止...
代码或建议的任何变化,最受大家欢迎的。
有什么不清楚的错误?你的html有一个标签没有结束标签。 – nhouser9
你如何解析这个?用XML解析器?这看起来不是有效的XML。不应该使用像JSoup这样的HTML解析器? –
@ nhouser9:但它确实 - 它是一个孤立的标签并自行关闭。 –