11
来源:互联网 发布:统计学中观测数据定义 编辑:程序博客网 时间:2024/06/05 04:25
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class TestBook {
/**
* @param args
*/
public static void main(String[] args) {
// 1.获得一个新 DocumentBuilderFactory 实例
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// 2.使用DocumentBuilderFactory 构建 DocumentBuilder
DocumentBuilder db;
String[] EachNode;
String book1name = null;
try {
db = dbf.newDocumentBuilder();
// 3.使用DocumentBuilder 的parse() 方法解析文件
// 4.将已解析的文档存储在Document对象中
Document document;
document = db.parse(new File("book.xml"));
// 5.使用 getElementsByTagName() 方法获得元素
NodeList bookList = document.getElementsByTagName("book");
EachNode = new String[bookList.getLength()];
System.out.println("bookList.getLength() "
+ bookList.getLength());
// 获得所有的子节点
for (int i = 0; i < bookList.getLength(); i++) {
System.out.println("*********第" + i + "记录**************");
Node n = bookList.item(i);
System.out.println(" RootNode: " + n.getNodeName());
NodeList chileNodeList = n.getChildNodes();
for (int j = 0; j < chileNodeList.getLength(); j++) {
Node chileNode = chileNodeList.item(j);
// 如果元素结点是ELEMENT_NODE则打印出来
// chileNode 此时可以是元素结点,文本结点,属性结点或其它类型的信息
if (chileNode.getNodeType() == 1) {
System.out.println(" NodeName: "
+ chileNode.getNodeName());
System.out.println(" NodeType: "
+ chileNode.getNodeType());
}
if (chileNode.getFirstChild() != null) {
// System.out.print(chileNode.getNodeName());
// System.out.println(":"
// + chileNode.getFirstChild().getNodeValue());
EachNode[j] = new String(chileNode.getFirstChild()
.getNodeValue());
System.out.println(" NodeValue: " + EachNode[j]);
System.out.println(" |------------");
}
}
}
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
}
解析xml原文件:book.xml
<?xml version="1.0" encoding="gb2312" ?>
<?xml-stylesheet type="text/xsl" href="bookxsl.xsl"?>
<books>
<book>
<id>1</id>
<bookname>数据库原理与应用</bookname>
<author>陈红</author>
<addtime>2001-04-05</addtime>
<price>29</price>
</book>
<book>
<id>2</id>
<bookname>会计学</bookname>
<author>刘景瑞</author>
<addtime>2003-04-05</addtime>
<price>29</price>
</book>
<book>
<id>3</id>
<bookname>Asp.net专业项目开发实例</bookname>
<author>[美]Hersh Bhasin</author>
<addtime>2002-04-05</addtime>
<price>29</price>
</book>
<book>
<id>4</id>
<bookname>XML编程实例</bookname>
<author>陈红,何川</author>
<addtime>2001-03-05</addtime>
<price>29</price>
</book>
<book>
<id>5</id>
<bookname>数据库原理与应用</bookname>
<author>陈红</author>
<addtime>2002-11-03</addtime>
<price>29</price>
</book>
<book>
<id>6</id>
<bookname>数据库原理与应用</bookname>
<author>陈红</author>
<addtime>2002-06-05</addtime>
<price>29</price>
</book>
<book>
<id>7</id>
<bookname>精通EJB</bookname>
<author>[美]Ed Roman</author>
<addtime>2003-01-02</addtime>
<price>29</price>
</book>
<book>
<id>8</id>
<bookname>java中间件编程</bookname>
<author>李水根</author>
<addtime>2003-01-12</addtime>
<price>29</price>
</book>
<book>
<id>9</id>
<bookname>jsp入门</bookname>
<author>不知道</author>
<addtime>2004-06-12</addtime>
<price>29</price>
</book>
<book>
<id>10</id>
<bookname>powerBuilder8.0</bookname>
<author>不知道</author>
<addtime>2002-11-08</addtime>
<price>29</price>
</book>
<book>
<id>11</id>
<bookname>Visual Studio.net Framework</bookname>
<author>[美]Julian Templeman,David Vitter</author>
<addtime>2003-10-23</addtime>
<price>29</price>
</book>
- 11
- 11
- 11
- 11
- 11
- 11
- 11
- 11
- 11
- 11
- 11
- 11
- 11
- 11
- 11
- 11
- 11
- 11
- 过去十年中10大优秀机器人产品
- 我的知识管理之路(4)—以期刊推动知识分享和跨部门交流
- Hibernate其它映射
- xm
- ajax 处理 xml 数据
- 11
- 让 ASP.NET 2.0 的 Sumbit Button 支持 禁用(灰化)提交
- mini2440 ubuntu kermit minicom
- 怎样优化J2ME程序当中的paint()函数
- mac framework
- 关于求最大公约数
- 600201股吧
- 关于session(转自javaEye)
- 用第三方dylib,在XCode下编译Cocoa程序