java解析html

时间:2025-04-01 11:37:37
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()); }