[Source Code]使用Dom和dom4j读取XML文件

package com.mycompany.xmltest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Iterator;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.dom4j.Attribute;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class XMLTest {

    public void readXMLByDom(File thefile){
        BufferedReader bufferedReader = null;
        DocumentBuilder builder = null;
        Document doc = null;
        try {
            bufferedReader = new BufferedReader(new FileReader(thefile));
        } catch ( FileNotFoundException e ) {
            e.printStackTrace();
        }
       
        InputSource inputsource = new InputSource(bufferedReader);
       
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            builder = factory.newDocumentBuilder();
        } catch ( ParserConfigurationException e ) {
            e.printStackTrace();
        }
       
        try {
            doc = builder.parse(inputsource);
//            doc = builder.parse(thefile);
        } catch ( SAXException e ) {
            e.printStackTrace();
        } catch ( IOException e ) {
            e.printStackTrace();
        } finally{
            try {
                bufferedReader.close();
            } catch ( IOException e ) {
                e.printStackTrace();
            }
        }
       
        NodeList nList_01 = doc.getChildNodes();
        Node node = null;
        System.out.println("****************** First Level Node*******************");
        for ( int i = 0; i < nList_01.getLength(); i++ ) {
            node = nList_01.item(i);
            String temp1 = node.getNodeName();
            System.out.println("Node " + (i+1) + " is :" + temp1);
           
            if(node.hasChildNodes()){
                //attributes
                if ( node.hasAttributes() ) {
                    NamedNodeMap namedNodeMap = node.getAttributes();
                    System.out.println("############## Begin Attributes of " + temp1.toUpperCase() + " : ##############");
                    for ( int j = 0; j < namedNodeMap.getLength(); j++ ) {
                        Node attrNode = namedNodeMap.item(j);
                        System.out.println("~~~~~~attrNode "+ (j+1) + " is: " + attrNode.getNodeName() + "  Value is: " + attrNode.getNodeValue());
                    }
                    System.out.println("############## End Attributes of " + temp1.toUpperCase() + " : ##############");
                }
               
                //child nodes
                System.out.println("================Begin Second Level Childern Under " + node.getNodeName().toUpperCase() + "=================");
                NodeList nList_02 = node.getChildNodes();
                for ( int j = 0; j < nList_02.getLength(); j++ ) {
                    node = nList_02.item(j);
                    String temp01 = node.getNodeName();
                    System.out.println("Node 0" + (j+1) + " is :" + temp01);
                   
                    if ( node.hasChildNodes() ) {
                        System.out.println("----------------Begin Third Level Childern Under " + node.getNodeName().toUpperCase() + "------------------");
                        NodeList nList_03 = node.getChildNodes();
                        for ( int k = 0; k < nList_03.getLength(); k++ ) {
                            node = nList_03.item(k);
                            System.out.println("Node 00" + (k+1) + " is :" + node.getNodeName() );
                            if ( !node.getNodeName().equals("#text") ) {
                                System.out.println("Text within this node is: " + node.getTextContent());
                            }
                        }
                        System.out.println("----------------End Third Level Childern Under " + temp01.toUpperCase() + "------------------");
                    }
                }
                System.out.println("=================End Second Level Childern Under " + temp1.toUpperCase() + "================");
            }
        }
    }
   
    public void readXMLByDom4j(File thefile){
//        DocumentFactory factory = DocumentFactory.getInstance();
//        SAXReader saxreader = new SAXReader(factory);
        BufferedReader bufferedreader = null;
        SAXReader saxreader = new SAXReader();
        org.dom4j.Document doc = null;
        try {
            bufferedreader = new BufferedReader(new FileReader(thefile));
        } catch ( FileNotFoundException e ) {
            e.printStackTrace();
        }

        try {
            doc = ( org.dom4j.Document ) saxreader.read(bufferedreader);
        } catch ( DocumentException e ) {
            e.printStackTrace();
        } finally{
            try {
                bufferedreader.close();
            } catch ( IOException e ) {
                e.printStackTrace();
            }
        }
       
        //iterate Nodes
        org.dom4j.Node node = null;
        int n = 1;
        Iterator it = doc.nodeIterator();
        System.out.println("========== iterate Nodes =========");
        while ( it.hasNext() ) {
            node = ( org.dom4j.Node ) it.next();
            System.out.println("Name of Node" +n++ + " is :" + node.getName());
            System.out.println("Node type name is : " + node.getNodeTypeName()+"/n");
        }
       
        //iterate Elements
        Element root = doc.getRootElement();
        System.out.println("Root Element : " + root.getName());
       
        //iterate Elements underneath Root Element
        Element eleOfRoot = null;
        int l = 1;
        Iterator eleIt = root.elementIterator();
        System.out.println("========== iterate Elements underneath Root Element =========");
        while ( eleIt.hasNext() ) {
            eleOfRoot = ( Element ) eleIt.next();
            System.out.println("Element " + l++ + " underneath Root is :" + eleOfRoot.getName());
           
            //grandchildren of Root Element
            if ( eleOfRoot.hasMixedContent() ) {
                Element grandchild = null;
                int p = 1;
                Iterator grandIt = eleOfRoot.elementIterator();
                System.out.println("@@@@@@@@@@@@@@@@@ iterate Elements underneath " + eleOfRoot.getName() + " @@@@@@@@@@@@@@");
                while ( grandIt.hasNext() ) {
                    grandchild = ( Element ) grandIt.next();
                    System.out.println("Element " + 0+ p++ + " underneath " + eleOfRoot.getName() + " is :" + grandchild.getName());
                }
                System.out.println("");
            }
        }
       
        //iterate Attribute of Root Element
        Attribute attr = null;
        int m=1;
        Iterator attrIt = root.attributeIterator();
        System.out.println("========== iterate Attributes of Root Element =========");
        while ( attrIt.hasNext() ) {
            attr = ( Attribute ) attrIt.next();
            System.out.println("Attribute " + m++ + " is : " + attr.getName() + " ~~~ Value = " + attr.getValue());
           
        }
       
    }
   
    public static void main( String[] args ) {
        XMLTest test= new XMLTest();
        File thefile = new File("F://build.xml");
        test.readXMLByDom(thefile);
        System.out.println("/n>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Below is with Dom4j<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
        test.readXMLByDom4j(thefile);
    }
}

阅读更多

没有更多推荐了,返回首页