java模拟浏览器解析dom,Java HTML/XML - 如何使用DOM解析器检查节点类型

import java.io.ByteArrayInputStream;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.xml.sax.InputSource;

public class Main {

public static void main(String[] args) throws Exception {

String xml = ""

+ ""

+ ""

+ "";

DocumentBuilderFactory documentFactory = DocumentBuilderFactory

.newInstance();

DocumentBuilder documentBuilder = null;

documentBuilder = documentFactory.newDocumentBuilder();

org.w3c.dom.Document doc = documentBuilder.parse(new InputSource(

new ByteArrayInputStream(xml.getBytes())));

doc.getDocumentElement().normalize();

NodeList nodeList0 = doc.getElementsByTagName("service");

NodeList nodeList1 = null;

NodeList nodeList2 = null;

System.out.println("Root element :"

+ doc.getDocumentElement().getNodeName());

for (int temp0 = 0; temp0 < nodeList0.getLength(); temp0++) {

Node node0 = nodeList0.item(temp0);

System.out.println("\nElement type :" + node0.getNodeName());

Element Service = (Element) node0;

if (node0.getNodeType() != Node.ELEMENT_NODE) {

continue;

}

System.out.println("name : " + Service.getAttribute("name"));

System.out.println("id : " + Service.getAttribute("id"));

nodeList1 = Service.getChildNodes();

for (int temp = 0; temp < nodeList1.getLength(); temp++) {

Node node1 = nodeList1.item(temp);

System.out.println("\nElement type :" + node1.getNodeName());

Element File = (Element) node1;

if (node1.getNodeType() != Node.ELEMENT_NODE) {

continue;

}

System.out.println("rootProfile:" + File.getAttribute("rootProfile"));

System.out.println("extension : " + File.getAttribute("extension"));

nodeList2 = File.getChildNodes();// colums

for (int temp1 = 0; temp1 < nodeList2.getLength(); temp1++) {

Element column = (Element) nodeList2.item(temp1);

NodeList nodeList4 = column.getChildNodes();

for (int temp3 = 0; temp3 < nodeList4.getLength(); temp3++) {

Element name = (Element) nodeList4.item(temp3);

if (name.getNodeType() != Node.ELEMENT_NODE) {

continue;

}

System.out.println("id:" + name.getAttribute("id"));

System.out.println("profileName : "

+ name.getAttribute("profileName"));

System.out.println("type : " + name.getAttribute("type"));

}

}

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值