用Dom4j来解析xml文件
package com.pk.xml;
import java.io.File;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Dom4j {
public static void main(String[] args) {
try {
//获得SAX解析器
SAXReader reader = new SAXReader();
//解析文件
File file = new File("D:\\项目\\me\\mobilelist.xml");
Document document = reader.read(file);
//获取跟节点
Element root = document.getRootElement();
//获得子节点列表
for(int i = 0 ; i<root.nodeCount();i++){
//取得某一个子节点
Element book = (Element) root.node(i);
//取得属性值
String type = book.attributeValue("type");
System.out.print(type+"\t");
String wap2 = book.node(0).getText();
String wedint = book.node(1).getText();
System.out.print(wap2+"\t");
System.out.println(wedint+"\t");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
相关文档:
test.html
———————————————————————————————————————& ......
声明
/// <summary>
/// XML文档
/// </summary>
XmlDocument xmldoc;
&n ......
xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<mobile-list>
<mobile type="Nokia2652">
<wap2>false</wap2>
<width>115</width>
</mobile>
<mobile type="Nokia2650">
......
大量SmipleXML函数可用来加载和解析大量XML文档。
1.simpleXML_load_file()函数来加载指定的XML文件到对象。如果加载文件时遇到问题,则返回FLASE。例:
book.xml文件:
<?xml version="1.0" standalone="yes"?>
<library>
<book>
<title>Pride and Prejudice</title>
< ......