xml文件读取,
xml文件:
[code="xml"]<?xml version="1.0" encoding="UTF-8"?>
A123000
四川省XX县XX镇XX路X段XX号
B1234
四川省XX市XX乡XX村XX组
B1234
四川省XX市XX乡XX村XX组
[/code]
[code="java"]//很明显该类是一个单例,先获取产生DocumentBuilder工厂,的工厂,在通过这个工厂产生一个DocumentBuilder,
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
//DocumentBuilder就是用来产生Document的
DocumentBuilder db=dbf.newDocumentBuilder();
//这个Document就是一个XML文件在内存中的镜像
Document doc=db.parse(new File("src/test.xml"));
System.out.println(XmlTest.class.getClassLoader().getResourceAsStream("test.xml"));
//在xml文件里,只有一个根元素,先把根元素拿出来看看
Element element=doc.getDocumentElement();
//元素的根节点
System.out.println("根节点:"+element.getTagName());
NodeList nodeList=doc.getElementsByTagName("VALUE");
System.out.println("VALUE节点长度(个数):"+nodeList.getLength());
//父节点
Node fatherNode=nodeList.item(0);
System.out.println("父节点:"+fatherNode.getNodeName());
//所有子节点
NodeList childNodes= fatherNode.getChildNodes();
System.out.println(""+childNodes.getLength());[/code]
遇到问题:为什么子节点长度为5???? 明明才有两个。 而且打印的时候显示的是 索引 2,4 才打印了。