DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance() ;
File file = new File(".\\WebRoot\\WEB-INF\\NewFile.xml");
DocumentBuilder builder = factory.newDocumentBuilder() ;
Document document = builder.parse(file) ;
NodeList nodelist = document.getElementsByTagName("employee") ;
for(int i = 0 ; i < nodelist.getLength() ; i++){
Element element = (Element)nodelist.item(i) ;
String right = element.getAttribute("right") ;
System.out.println(right) ; builder.parse(file)中返回一个Document 引用,而且Document是个接口,
那么它 具体的工作(document.getElementsByTagName("employee") ;)
是怎么实现的呢???????????????????????????????????
File file = new File(".\\WebRoot\\WEB-INF\\NewFile.xml");
DocumentBuilder builder = factory.newDocumentBuilder() ;
Document document = builder.parse(file) ;
NodeList nodelist = document.getElementsByTagName("employee") ;
for(int i = 0 ; i < nodelist.getLength() ; i++){
Element element = (Element)nodelist.item(i) ;
String right = element.getAttribute("right") ;
System.out.println(right) ; builder.parse(file)中返回一个Document 引用,而且Document是个接口,
那么它 具体的工作(document.getElementsByTagName("employee") ;)
是怎么实现的呢???????????????????????????????????
然后就知道对应的方法是如何操作的。
估计也是一个哈希表的操作,
我从DocumentBuilderFactory 开始往下找,没找到一个实现类。。
不是接口就是抽象类,即使是抽象类,也没有返回Document的对应实例
所以是以接口的形式推出来,任何第三方的组织都可以去实现这个接口里所有的方法,Dom4j,jDom都是这么做的比较好用的框架。至于你关心的为什么DocumentBuilderFactory.newInstance()就可以解析xml返回解析完毕的实现了DOM接口的对象,是因为JDK中由Sun自己提供了一个实现类,全类名是“com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl”它产生Factory实例,再由Factory产生DocumentBuilder实例,然后解析生成DOM对象其实你只要类比JDBC,就能理解得更深一些!重点是什么是接口……※