[Reprinted] read xml file on the learning Java

Talking about the content of this article is on the Java read xml file in the relevant the content of the article can be divided into three parts, namely, Java classes, xml files Yiji the final output.

package com.java.test;    

import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;    

public class JavaReadXml
   // Document Can be viewed as XML in the memory of a mirror  , So once you get this Document means that you can
   //  Memory operations on XML operation  , First of all, the first step to obtain XML-related  Document
    private Document doc = null;    

    public void init(String xmlFile) throws Exception
         //  It is clear that the class is a singleton, first get the resulting  DocumentBuilder Factory
        //  Factory, this factory produces a  DocumentBuilder,
         // DocumentBuilder It is used to create the Document
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            //  This Document is a  XML The file is in memory of mirror
        doc = db.parse(new File(xmlFile));

    //  This method is responsible for the contents of the XML file are displayed
    public void viewXML(String xmlFile) throws Exception
            //  In an XML file  , There is only one root element the root element, first take out a look
            Element element = doc.getDocumentElement();
            System.out.println(" The root element for the  :" + element.getTagName());    

            NodeList nodeList = doc.getElementsByTagName("person");
            System.out.println("book The length of the chain nodes  :" + nodeList.getLength());    

            Node fatherNode = nodeList.item(0);
            System.out.println(" The parent node to  :" + fatherNode.getNodeName());    

            //  The parent node of the properties out
            NamedNodeMap attributes = fatherNode.getAttributes();    

            for (int i = 0; i < attributes.getLength(); i++)
                  Node attribute = attributes.item(i);
                  System.out.println("book The property name is  :" + attribute.getNodeName()
                  + "  The corresponding property value for the  :" + attribute.getNodeValue());

            NodeList childNodes = fatherNode.getChildNodes();
            for (int j = 0; j < childNodes.getLength(); j++)
                  Node childNode = childNodes.item(j);
                  //  If this node is a Element, then the value
                  if (childNode instanceof Element)
                        // System.out.println(" Child nodes named  :"+childNode.getNodeName()+" Corresponds to the value of  "+childNode.getFirstChild().getNodeValue());
                        System.out.println(" Child nodes named  :" + childNode.getNodeName()
                        + " Corresponds to the value of  " + childNode.getFirstChild().getNodeValue());


    public static void main(String[] args) throws Exception
          JavaReadXml parse = new JavaReadXml();    

          //  My XML file
<?xml version="1.0" encoding="UTF-8"?>
        <version> China mailing press  </version>
        <version> Tsinghua University Press  </version>


Root elements: book
book node in the chain length: 2
Parent node is: person
Child nodes named: first corresponding values wang
Child nodes named: last corresponding to the value of laohu
Child nodes called: age corresponds to a value of 25
Child nodes named: version corresponding to the value of China Posts and Telecommunications Press

分类:Java 时间:2010-03-29 人气:215
blog comments powered by Disqus


iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 闽ICP备15018612号

processed in 0.375 (s). 12 q(s)