[Reprinted] read xml file on the learning Java
Advertisements
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
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 ...












