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的抽象方法,代码中是如何何时又实现这个方法了列?
{
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的抽象方法,代码中是如何何时又实现这个方法了列?
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;
}
这个应该是用JAVA去解析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);返回的返回值是什么类型?
Document d=db.parse(new File("B.xml")); 这段代码db.parse(new File("B.xml")); 若是没有经过上转型或下转型的话,那么d变量引用的是什么呢?Document是抽象类根本不会有Document对象。
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 中(其中的所有属性都有简单的字符串值),可以使用直接访问属性值的方法,这既安全又便捷。
将给定文件的内容解析为一个 XML 文档,并且返回一个新的 DOM Document 对象。
NodeList 的方法 item(int index)
返回集合中的第 index 个项。
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
//这是一个工厂累,通过自身的静态方法newInstance()来获取到对象。DocumentBuilder db=dbf.newDocumentBuilder();
// DocumentBuilder虽然是一个接口 [java可以定义接口变量的],但接口变量初始化的时候是通过工厂对象dbf的newDocumentBuilder()方法来获取到一个该接口的实现类的一个实例。Document d=db.parse(new File("B.xml"));
//这个就是获取到xml文档描述对象。
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();
}
}
查API写
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); 是没有问题的
但是
DocumentBuilder db=dbf.newDocumentBuilder();我们怎么可以写出来列,dbf只是个接口变量,它所引用的对象通过API根本无法知道,怎么可以知道它引用的这个实体已经实现了newDocumentBuilder();这个抽象的方法呢?
要是哪个高手知道请回答我谢谢,翘首以盼!
只需要回答上面这个问题就行了,你们看问题太不认真了。还是期待高手能解决我的问题。闹眼子的结贴不给分的啊