java解析html

html 在浏览器使用js可以很简单的解析出来,获取自己所需要的内容或者值。
在Java代码里我们可以使用:

javax.xml.parsers.DocumentBuilder

来解析html,来获取需要的内容或者值

来个示例:

    public void test6() throws Exception {
    	//获取DocumentBuilder 
        DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        //需要解析html
        String html = "tester1
10
"
; //这里需要一个完整的开始标签和结束标签,可以使用其他的,如果你的html是完整的,可以不加。 String html = ""+html+""; 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

你可能感兴趣的:(java,html,开发语言)