public static void main(String args[])
{
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder db=dbf.newDocumentBuilder();
Document d=db.parse(new File("B.xml"));
NodeList nl=d.getElementsByTagName("table1");
NodeList n2=d.getElementsByTagName("table2");

int len1=nl.getLength();
int len2=n2.getLength();
System.out.println(len1);
for(int i=0;i<len1;i++)
{
Element stu=(Element)nl.item(i); NodeList e1=stu.getElementsByTagName("reportrow");
int len11=e1.getLength();
System.out.println(len11);
for(int j=0;j<len11;j++)
{
Element report=(Element)e1.item(j);
Node project=report.getElementsByTagName("项目").item(0);
String project1=project.getFirstChild().getNodeValue();
System.out.println(project1);
}

}
}
catch(Exception e)
{
e.printStackTrace();
}
}
主要解释红色部分谢谢
DocumentBuilderFactory 、DocumentBuilder、Document都是接口,newDocumentBuilder()是DocumentBuilderFactory的抽象方法,代码中是如何何时又实现这个方法了列?

解决方案 »

  1.   

    check this out:http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/parsers/DocumentBuilderFactory.html
      

  2.   

    源代码我看过了,dbf.newDocumentBuilder();dbf.newDocumentBuilder(); db.parse(new File("B.xml"));这些方法未经过上转型前返回的对象,还是请高手指教,DocumentBuilderFactory ,DocumentBuilder,Document都是接口源码我已经看过了还是不怎么懂
      

  3.   

    api应该也没有说它上转型前的类型吧?只给返回类型
      

  4.   

    这是用Document对象解析XML的代码,java api有具体解释,你可以去看看。
      

  5.   

    api里面也没有说上转型前的类型,只告诉了返回类型吧!呵呵,还是请直截了当的告诉我得了。
      

  6.   

    这个……看源码,API 都可以啊……
      

  7.   

    DocumentBuilderFactory.newInstance(); 方法源码 public static DocumentBuilderFactory newInstance() {
            try {
                return (DocumentBuilderFactory) FactoryFinder.find(
                    /* The default property name according to the JAXP spec */
                    "javax.xml.parsers.DocumentBuilderFactory",
                    /* The fallback implementation class name */
                    "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
            } catch (FactoryFinder.ConfigurationError e) {
                throw new FactoryConfigurationError(e.getException(),
                                                    e.getMessage());
            }    }db.parse(new File("B.xml"));
    方法源码
    public Document parse(File f) throws SAXException, IOException {
            if (f == null) {
                throw new IllegalArgumentException("File cannot be null");
            }        //convert file to appropriate URI, f.toURI().toASCIIString() 
            //converts the URI to string as per rule specified in
            //RFC 2396,
            InputSource in = new InputSource(f.toURI().toASCIIString());   
            return parse(in);
        }Element stu=(Element)nl.item(i);
    接口的方法
    public Node item(int index);
    实现类的方法(IIOMetadataNode)    public Node item(int index) {
            if (index < 0) {
                return null;
            }        Node child = getFirstChild();
            while (child != null && index-- > 0) {
                child = child.getNextSibling();
            }
            return child;
        }
      

  8.   

    感觉还是去看下相关的API,
    这个应该是用JAVA去解析XML的,可以去看下别的相关的解析程序!
    这块我涉及到比较少,
      

  9.   

    db.parse(new File("B.xml")); 
    方法源码 
    public Document parse(File f) throws SAXException, IOException { 
            if (f == null) { 
                throw new IllegalArgumentException("File cannot be null"); 
            }         //convert file to appropriate URI, f.toURI().toASCIIString() 
            //converts the URI to string as per rule specified in 
            //RFC 2396, 
            InputSource in = new InputSource(f.toURI().toASCIIString());  
            return parse(in); 
        } 
    中的parse(in);返回的返回值是什么类型?
      

  10.   

    呵呵,返回类型肯定不是Document的,因为Document是接口,其中它的getElementsByTagName();也是抽象的吧
      

  11.   

    ”返回类型肯定不是Document的“......当然是取决于调用方法的对象作为方法的retrun类型,毫无疑问,就是Document如果你要问具体是哪个实现类,我无法回答你,我没用过这些东西
      

  12.   

    15楼的兄弟
    Document d=db.parse(new File("B.xml")); 这段代码db.parse(new File("B.xml")); 若是没有经过上转型或下转型的话,那么d变量引用的是什么呢?Document是抽象类根本不会有Document对象。
      

  13.   

    "Document是抽象类根本不会有Document对象。"呵呵......
      

  14.   

    public abstract class DocumentBuilder extends Object定义 API, 使其从 XML 文档获取 DOM 文档实例。使用此类,应用程序员可以从 XML 获取一个 Document。此类的实例可以从 DocumentBuilderFactory.newDocumentBuilder() 方法获取。获取此类的实例之后,将可以从各种输入源解析 XML。这些输入源有 InputStreams、Files、URL 和 SAX InputSources。注意,此类重用了 SAX API 中的一些类。这并不要求底层 DOM 实现的实现者使用 SAX 解析器将 XML 文档解析为 Document。它仅要求该实现使用这些现有的 API 与应用程序交流。
    public abstract class DocumentBuilderFactory extends Object定义工厂 API,使应用程序能够从 XML 文档获取生成 DOM 对象树的解析器。 
    public interface Document extends NodeDocument 接口表示整个 HTML 或 XML 文档。从概念上讲,它是文档树的根,并提供对文档数据的基本访问。 因为元素、文本节点、注释、处理指令等不能存在于 Document 的上下文之外,所以 Document 接口还包含所需的创建这些对象的工厂方法。所创建的 Node 对象具有 ownerDocument 属性,该属性将 Node 对象与创建这些对象时的上下文所属的 Document 关联起来。
    public interface Element extends NodeElement 接口表示 HTML 或 XML 文档中的一个元素。元素可能有与它们相关的属性;由于 Element 接口继承自 Node,所以可以使用一般 Node 接口属性 attributes 来获得元素所有属性的集合。Element 接口上有通过名称获得 Attr 对象或通过名称获得属性值的方法。在 XML 中(其中的属性值可能包含实体引用),应该获得 Attr 对象来检查表示属性值的可能相当复杂的子树。另一方面,在 HTML 中(其中的所有属性都有简单的字符串值),可以使用直接访问属性值的方法,这既安全又便捷。 
      

  15.   

    DocumentBuilder的方法 parse(File f) 
              将给定文件的内容解析为一个 XML 文档,并且返回一个新的 DOM Document 对象。 
    NodeList  的方法 item(int index) 
              返回集合中的第 index 个项。 
      

  16.   

    我觉得还是看源码和API最清楚
      

  17.   

    DocumentBuilder db = dbf.newDocumentBuilder();
    System.out.println(db.getClass());
    Document d = db.parse(new File("B.xml"));
    System.out.println(d.getClass());返回前的类型:
    class com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl
    class com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl
      

  18.   

    多花点时间看源码和API,这样问效果不好
      

  19.   

    建议楼主自己多看看API,会有很多收获的。。
      

  20.   

    这是java中的接口回调的使用。但在你定义的变量初始化的时候一定是返回的是他的一个实现类。DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); 
        //这是一个工厂累,通过自身的静态方法newInstance()来获取到对象。DocumentBuilder db=dbf.newDocumentBuilder(); 
        // DocumentBuilder虽然是一个接口 [java可以定义接口变量的],但接口变量初始化的时候是通过工厂对象dbf的newDocumentBuilder()方法来获取到一个该接口的实现类的一个实例。Document d=db.parse(new File("B.xml")); 
         //这个就是获取到xml文档描述对象。
      

  21.   

    DOM解析,,可以去看看相对于较基础的帮助文档吧!!那里肯定有解释的!!
      

  22.   


    public static void main(String args[]) 

    //声明变量dbf的方法是从DocumentBuilderFactory这个类中产生一个DocumentBuilderFactory的实例。
    //DocumentBuilderFactory 属于工厂类——就是很多拥有共同特征的类型的集合
    //newInstance()参照java的反射API
    DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); 
    try 

    DocumentBuilder db=dbf.newDocumentBuilder(); 
    //这里是父类引用指向子类对象
    //java的多态
    Document d=db.parse(new File("B.xml")); NodeList nl=d.getElementsByTagName("table1"); 
    NodeList n2=d.getElementsByTagName("table2"); int len1=nl.getLength(); 
    int len2=n2.getLength(); 
    System.out.println(len1); 
    for(int i=0;i <len1;i++) 

    Element stu=(Element)nl.item(i); NodeList e1=stu.getElementsByTagName("reportrow"); 
    int len11=e1.getLength(); 
    System.out.println(len11); 
    for(int j=0;j <len11;j++) 

    //Node item(int index)返回集合中的第 index 个项。如果 index 大于或等于此列表中的节点数,则返回 null。Element report=(Element)e1.item(j); 
    Node project=report.getElementsByTagName("项目").item(0); 
    String project1=project.getFirstChild().getNodeValue(); 
    System.out.println(project1); 
    } } 

    catch(Exception e) 

    e.printStackTrace(); 

      

  23.   

    呵呵,感谢大家的回帖,只是我觉得目前我的问题还是没有得到解决。呵呵,关于上下转型的概念我认为自己还是理解的,我发的这段代码是很通用的,我只是想要是我们没有不熟这段代码,用API能写出来吗?
    查API写
    DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); 是没有问题的
    但是
    DocumentBuilder db=dbf.newDocumentBuilder();我们怎么可以写出来列,dbf只是个接口变量,它所引用的对象通过API根本无法知道,怎么可以知道它引用的这个实体已经实现了newDocumentBuilder();这个抽象的方法呢?
    要是哪个高手知道请回答我谢谢,翘首以盼!
      

  24.   

    我终于知道为什么了,DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); 这句表明DocumentBuilderFactory.newInstance();所生成的类肯定是DocumentBuilderFactory的子类经过上转型后的对象,既然是DocumentBuilderFactory的子类,又能实例化,说明不是抽象类了已经。就代表实现了DocumentBuilderFactory中的所有抽象方法,所以能够有DocumentBuilder db=dbf.newDocumentBuilder();
      

  25.   

    Element stu=(Element)nl.item(i);这段代码就不知道怎么来了的,我想知道为什么喜欢用上转型,然后来个下转型这样还把问题复杂话了我感觉哦
      

  26.   

    DocumentBuilderFactory、DocumentBuilder不是接口,是abstract类,DocumentBuilderFactory.newInstance()是DocumentBuilderFactory中的static方法,当然可以直接调用,以生成一个instance。
      

  27.   

    Element stu=(Element)nl.item(i);这段代码中n1.item(i)为什么能强制转化为Element,我想知道为什么喜欢用上转型,然后来个下转型这样还把问题复杂话了我感觉哦
    只需要回答上面这个问题就行了,你们看问题太不认真了。还是期待高手能解决我的问题。闹眼子的结贴不给分的啊
      

  28.   

    楼主,我的问题跟你相同啊,dom包了全都是接口,但可以直接通过接口调用方法,并没有看到有哪个类实现了这些接口,这是怎么回事啊,接口中的方法实例是在哪个类里实现的啊?