[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
    {
            this.init(xmlFile);
            //  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();
            System.out.println(childNodes.getLength());
            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
          parse.viewXML("person.xml");
    }
}   
<?xml version="1.0" encoding="UTF-8"?>
<book>
    <person>
        <first>wang</first>
        <last>laohu</last>
        <age>25</age>
        <version> China mailing press  </version>
    </person>
    <person>
        <first>li</first>
        <last>junjia</last>
        <age>24</age>
        <version> Tsinghua University Press  </version>
    </person>
</book>

Output:

Root elements: book
book node in the chain length: 2
Parent node is: person
9
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 人气:212
分享到:
blog comments powered by Disqus

相关文章

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

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

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.676 (s). 13 q(s)