importorg.w3c.dom.*;importjavax.xml.parsers.*;importjava.io.*;publicclassParse{//Document可以看作是XML在内存中的一个镜像,那么一旦获取这个Document 就意味着可以通过对//内存的操作来实现对XML的操作,首先第一步获取XML相关的DocumentprivateDocument doc=null;publicvoidinit(String xmlFile)throwsException{//很明显该类是一个单例,先获取产生DocumentBuilder工厂//的工厂,在通过这个工厂产生一个DocumentBuilder,//DocumentBuilder就是用来产生Document的DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();//这个Document就是一个XML文件在内存中的镜像doc=db.parse(newFile(xmlFile));
}//该方法负责把XML文件的内容显示出来publicvoidviewXML(String xmlFile)throwsException{this.init(xmlFile);//在xml文件里,只有一个根元素,先把根元素拿出来看看Element element=doc.getDocumentElement();
System.out.println("根元素为:"+element.getTagName());
NodeList nodeList=doc.getElementsByTagName("dbstore");
System.out.println("dbstore节点链的长度:"+nodeList.getLength());
Node fatherNode=