欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java:DocumentBuilderFactory调用XML的方法实例

程序员文章站 2023-12-01 13:57:34
首先得到:得到 dom 解析器的工厂实例      documentbuilderfactory domfac=docum...

首先得到:得到 dom 解析器的工厂实例      documentbuilderfactory domfac=documentbuilderfactory.newinstance();

然后从 dom 工厂获得 dom 解析器

 documentbuilder dombuilder=domfac.newdocumentbuilder();

 

)把要解析的 xml 文档转化为输入流,以便 dom 解析器解析它

inputstream is= new  fileinputstream("test1.xml");        

( 4 )解析 xml 文档的输入流,得到一个 document

 document doc=dombuilder.parse(is);

( 5 )得到 xml 文档的根节点

element root=doc.getdocumentelement();

( 6 )得到节点的子节点

  nodelist books=root.getchildnodes();

复制代码 代码如下:

package com.st.demo;  

import java.io.file;  
import java.io.fileinputstream;  
import java.io.inputstream;  

import javax.xml.parsers.documentbuilder;  
import javax.xml.parsers.documentbuilderfactory;  

import org.w3c.dom.document;  
import org.w3c.dom.element;  
import org.w3c.dom.node;  
import org.w3c.dom.nodelist;  

public class xmlreader {  
    public static void main(string[] args) {  
        xmlreader reader = new xmlreader();  
    }  
    public xmlreader(){  
        documentbuilderfactory domfac = documentbuilderfactory.newinstance();  
        try {  
            documentbuilder dombuilder = domfac.newdocumentbuilder();  
            inputstream is = new fileinputstream(new file("d:/test1.xml"));  
            document doc = dombuilder.parse(is);  
            element root = doc.getdocumentelement();  
            nodelist books = root.getchildnodes();  
            if(books!=null){  
                for (int i = 0; i < books.getlength(); i++) {  
                    node book = books.item(i);  
                     if(book.getnodetype()==node.element_node) {  
                            //(7)取得节点的属性值  
                            string email=book.getattributes().getnameditem("email").getnodevalue();  
                            system.out.println(email);  
                            //注意,节点的属性也是它的子节点。它的节点类型也是node.element_node  
                            //(8)轮循子节点  
                            for(node node=book.getfirstchild();node!=null;node=node.getnextsibling()) {  
                                if(node.getnodetype()==node.element_node) {  
                                    if(node.getnodename().equals("name")) {  
                                        string name=node.getnodevalue();  
                                        string name1=node.getfirstchild().getnodevalue();  
                                        system.out.println(name);  
                                        system.out.println(name1);  
                                    }  
                                    if(node.getnodename().equals("price")) {  
                                        string price=node.getfirstchild().getnodevalue();  
                                        system.out.println(price);  
                                    }  
                                }  
                            }  
                     }  
                }  
            }  
        } catch (exception e) {  
            // todo auto-generated catch block  
            e.printstacktrace();  
        }  

    }