html 在浏览器使用js可以很简单的解析出来,获取自己所需要的内容或者值。
在Java代码里我们可以使用:
javax.xml.parsers.DocumentBuilder
来解析html,来获取需要的内容或者值
来个示例:
public void test6() throws Exception {
//获取DocumentBuilder
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
//需要解析html
String html = "<span id='spanName_Q85fcFzaEY' attendee-id='5490'>tester1</span><div id='num_Q85fcFzaEY' class='attendee'>10</div>";
//这里需要一个完整的开始标签和结束标签,可以使用其他的,如果你的html是完整的,可以不加。
String html = "<a>"+html+"</a>";
Document document = documentBuilder.parse(new ByteArrayInputStream( html.getBytes()));
System.out.println(document.getElementsByTagName("span").item(0).getAttributes().getNamedItem("attendee-id").getNodeValue());
System.out.println(document.getElementsByTagName("div").item(0).getTextContent());
}
输出结果:
5490
10