[Reprinted] read xml file on the learning Java

sponsored links
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
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of [Reprinted] read xml file on the learning Java

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • Servlet brief introduction

    Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...

  • First Hibernate Example

    Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

  • Hibernate's lazy strategy

    hibernate Lazy strategy can be used in: <class> tag, it can be true / false Tags can <PROPERTY> values true / false type of necessary tools to enhance <set> <list> can tag values true / false / extra <many-to-one> <on ...

blog comments powered by Disqus
Recent Entries
Tag Cloud
Random Entries