关于Java读取xml文件
2009-07-06 14:47
274 查看
package com.java.test;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
public class JavaReadXml {
// Document可以看作是XML在内存中的一个镜像,那么一旦获取这个Document 就意味着可以通过对
// 内存的操作来实现对XML的操作,首先第一步获取XML相关的Document
private Document doc = null;
public void init(String xmlFile) throws Exception {
// 很明显该类是一个单例,先获取产生DocumentBuilder工厂
// 的工厂,在通过这个工厂产生一个DocumentBuilder,
// DocumentBuilder就是用来产生Document的
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// 这个Document就是一个XML文件在内存中的镜像
doc = db.parse(new File(xmlFile));
}
// 该方法负责把XML文件的内容显示出来
public void viewXML(String xmlFile) throws Exception {
this.init(xmlFile);
// 在xml文件里,只有一个根元素,先把根元素拿出来看看
Element element = doc.getDocumentElement();
System.out.println("根元素为:" + element.getTagName());
NodeList nodeList = doc.getElementsByTagName("person");
System.out.println("book节点链的长度:" + nodeList.getLength());
Node fatherNode = nodeList.item(0);
System.out.println("父节点为:" + fatherNode.getNodeName());
// 把父节点的属性拿出来
NamedNodeMap attributes = fatherNode.getAttributes();
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
System.out.println("book的属性名为:" + attribute.getNodeName()
+ " 相对应的属性值为:" + attribute.getNodeValue());
}
NodeList childNodes = fatherNode.getChildNodes();
System.out.println(childNodes.getLength());
for (int j = 0; j < childNodes.getLength(); j++) {
Node childNode = childNodes.item(j);
// 如果这个节点属于Element ,再进行取值
if (childNode instanceof Element) {
// System.out.println("子节点名为:"+childNode.getNodeName()+"相对应的值为"+childNode.getFirstChild().getNodeValue());
System.out.println("子节点名为:" + childNode.getNodeName()
+ "相对应的值为" + childNode.getFirstChild().getNodeValue());
}
}
}
public static void main(String[] args) throws Exception {
JavaReadXml parse = new JavaReadXml();
// 我的XML文件
parse.viewXML("person.xml");
}
}
Xml文件
<?xml version="1.0" encoding="UTF-8"?>
<book>
<person>
<first>wang</first>
<last>laohu</last>
<age>25</age>
<version>中国邮电出版社</version>
</person>
<person>
<first>li</first>
<last>junjia</last>
<ag>24</age>
<version>清华大学出版社</version>
</person>
</book>
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
public class JavaReadXml {
// Document可以看作是XML在内存中的一个镜像,那么一旦获取这个Document 就意味着可以通过对
// 内存的操作来实现对XML的操作,首先第一步获取XML相关的Document
private Document doc = null;
public void init(String xmlFile) throws Exception {
// 很明显该类是一个单例,先获取产生DocumentBuilder工厂
// 的工厂,在通过这个工厂产生一个DocumentBuilder,
// DocumentBuilder就是用来产生Document的
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// 这个Document就是一个XML文件在内存中的镜像
doc = db.parse(new File(xmlFile));
}
// 该方法负责把XML文件的内容显示出来
public void viewXML(String xmlFile) throws Exception {
this.init(xmlFile);
// 在xml文件里,只有一个根元素,先把根元素拿出来看看
Element element = doc.getDocumentElement();
System.out.println("根元素为:" + element.getTagName());
NodeList nodeList = doc.getElementsByTagName("person");
System.out.println("book节点链的长度:" + nodeList.getLength());
Node fatherNode = nodeList.item(0);
System.out.println("父节点为:" + fatherNode.getNodeName());
// 把父节点的属性拿出来
NamedNodeMap attributes = fatherNode.getAttributes();
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
System.out.println("book的属性名为:" + attribute.getNodeName()
+ " 相对应的属性值为:" + attribute.getNodeValue());
}
NodeList childNodes = fatherNode.getChildNodes();
System.out.println(childNodes.getLength());
for (int j = 0; j < childNodes.getLength(); j++) {
Node childNode = childNodes.item(j);
// 如果这个节点属于Element ,再进行取值
if (childNode instanceof Element) {
// System.out.println("子节点名为:"+childNode.getNodeName()+"相对应的值为"+childNode.getFirstChild().getNodeValue());
System.out.println("子节点名为:" + childNode.getNodeName()
+ "相对应的值为" + childNode.getFirstChild().getNodeValue());
}
}
}
public static void main(String[] args) throws Exception {
JavaReadXml parse = new JavaReadXml();
// 我的XML文件
parse.viewXML("person.xml");
}
}
Xml文件
<?xml version="1.0" encoding="UTF-8"?>
<book>
<person>
<first>wang</first>
<last>laohu</last>
<age>25</age>
<version>中国邮电出版社</version>
</person>
<person>
<first>li</first>
<last>junjia</last>
<ag>24</age>
<version>清华大学出版社</version>
</person>
</book>
相关文章推荐
- 关于Java 工程读取xml文件 运行 伪属性名的错误
- 关于Java读取xml文件的学习
- 关于Java读取xml文件的学习
- 关于Java读取xml文件的学习
- JAXB注解 java 关于xml的注解,自动生成xml文件 - @XML***
- java读取XML文件的方法
- 通过xStream读取xml文件生成Java对象和Java对象生成xml文件
- java解析、读取xml文件,org.w3c.dom.Document
- Java中关于如何读取不同路径下图片/文件
- 关于如何读取XML文件的一个简单方法
- 关于JAVA中怎么读取和修改.properties文件中的值
- 关于获取放在src文件夹下读取xml文件的总结
- JAVA读取XML文件
- Java读取xml文件方法
- 关于java 中流读取时遇到的阻塞和返回-1的问题探究
- Java读取xml文件的四种方法
- 用JAVA读取XML文件
- 对java中关于文件读取方法的比较
- 用JAVA读取XML文件
- 『原创』关于如何在ppc模拟器中读取本地xml文件的办法