- dom4j 解析:文档对象模型解析,是W3C指定的一套规范标准。需要把整个文档读取到内存中,占用内存大,解析慢,但是访问效率高,增删改查快。适合解析小型文档。
- SAX 解析:基于事件驱动解析文档,边读边解析,不必解析整个文档,解析速度快,但是访问效率低,只能从开始顺序解析。
1、dom4j 解析
首先给出以下案例使用的 xmlTest.xml 文件,如下:
<?xml version="1.0" encoding="UTF-8"?>
<city>
<!--文档的根city已经创建。-->
<company unitId="89adf979asd" unitCode="xiaoCreate" unitName="上市公司" cityArea="北京海淀">
<unitId>89adf979asd</unitId>
<unitCode>xiaoCreate</unitCode>
<unitName>上市公司1</unitName>
<cityArea>北京海淀</cityArea>
</company>
<company unitId="wadfa3asd" unitCode="xiaoBuild">
<unitId>89adf979asd</unitId>
<unitCode>xiaoBuild</unitCode>
<unitName>上市公司2</unitName>
<cityArea>北京朝阳</cityArea>
</company>
<person id="123456">
<name>admin</name>
<gender>male</gender>
<age>25</age>
<addr>广东广州</addr>
</person>
<company unitId="44dfa3ahg">
<unitId>hfs3f979a23</unitId>
<unitCode>xiaoadmin</unitCode>
<unitName>上市公司3</unitName>
<cityArea>北京丰台</cityArea>
</company>
<person id="789012">
<name>agent</name>
<gender>female</gender>
<age>24</age>
<addr>湖南长沙</addr>
</person>
</city>
获取整个文档内容
递归循环输出节点内容
import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Dom4jReadXml {
public static void main(String[] args) throws Exception {
readXML();
}
public static void readXML() throws Exception {
// 创建saxReader对象
SAXReader reader = new SAXReader();
// 通过read方法读取一个文件 转换成Document对象
Document document = reader.read(new File("E:/temptest/xmlTest.xml"));
//获取根节点元素对象
Element node = document.getRootElement();
//遍历指定元素节点下所有的元素节点
listNodes(node);
//如果指定元素节点有多个,则只按顺序找第一个
// listNodes(node.element("person"));
}
/**
* 遍历当前节点元素下面的所有(元素的)子节点
*/
@SuppressWarnings("unchecked")
public static void listNodes(Element node) {
if(node.getName().equals("company")||node.getName().equals("city")) {
System.out.println("当前节点的名称::" + node.getName());
}else {
System.out.print("当前节点的名称::" + node.getName());
}
// 获取当前节点的所有属性节点
List<Attribute> list = node.attributes();
// 遍历属性节点
for (Attribute attr : list) {
System.out.println("属性:" + attr.getName()
+ "---" + attr.getValue());
}
if (!(node.getTextTrim().equals(""))) {
System.out.println("===" + node.getText());
}
// 当前节点下面子节点迭代器
Iterator<Element> it = node.elementIterator();
while (it.hasNext()) {
Element e = it.next();
// 对子节点进行遍历
listNodes(e);
}
}
}
获取指定的某类元素节点的内容
import java.io.File;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Dom4jReadXml {
public static void main(String[] args) throws Exception {
readXML();
}
public static void readXML() throws Exception {
// 创建saxReader对象
SAXReader reader = new SAXReader();
// 通过read方法读取一个文件 转换成Document对象
Document document = reader.read(new File("E:/temptest/xmlTest.xml"));
//获取根节点元素对象
Element node = document.getRootElement();
readCompany(node);
}
@SuppressWarnings("unchecked")
public static void readCompany(Element node) {
// 获取node节点中,第一个子节点的元素名称为company的元素节点。
Element company = node.element("company"); //只获取了第一个company元素节点
System.out.println(company.getName()+"节点的属性如下:");
List<Attribute> list = company.attributes();
for (Attribute attr : list) {
System.out.println(attr.getText() + "-----" + attr.getName()
+ "---" + attr.getValue());
}
// 获取node元素节点的下一级的所有company子元素节点 。
List<Element> companies = node.elements("company");//获取了node节点下的所有 company元素节点
System.out.println(company.getName()+"节点的子元素节点如下:");
int i = 1;
for (Element com : companies) {
System.out.println("第"+(i++)+"个公司资料开始");
System.out.println("节点名称为:"+com.getName());
List<Element> eles = com.elements();//获取当前节点的下一级所有子元素节点
for(Element ele : eles) {
System.out.println(ele.getName()+"=="+ele.getText());
}
}
}
}
获取指定的某个元素节点的内容
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class XmlCreateAndRead {
public static void main(String[] args) {
//从xml文件读出展示到页面
findByUnitid("wadfa3asd");
}
public static void findByUnitid(String unitId) {
/*
* 1. 得到Docuemnt
* 2. 给出xpath表达式
* 3. 调用docuemnt的方法进行xpath查询,得到Element
* 4. 把Element封装成对象,返回!
*/
try {
/*
* 1. 得到Docuembnt
*/
// 创建解析器
SAXReader reader = new SAXReader();
// 调用读方法,得到Document
Document doc = reader.read("E:\\temptest\\xmlTest.xml");
/*
* 2. 准备xpath
* //开头表示没有深的限制,可以在文档查询子元素、子元素的子元素!
* []中放的叫谓语,其实就是查询条件
* @unitId表示unitId属性,限定其必须等于方法参数unitId
*/
String xpath = "//company[@unitId='" + unitId + "']";
/*
* 3. 调用document方法完成查询
*/
Element companyEle = (Element)doc.selectSingleNode(xpath);
if(companyEle == null) {
System.out.println("未找到数据");
}
/*
* 4. 输出元素节点内容 或 把元素节点内容封装成对象返回
*/
// 获取company元素节点的属性值
System.out.println(companyEle.getName()+"节点的属性如下:");
List<Attribute> list = companyEle.attributes();
for (Attribute attr : list) {
System.out.println(attr.getText() + "-----" + attr.getName()
+ "---" + attr.getValue());
}
// 获取company元素节点的下一级子节点及值
List<Element> eles = companyEle.elements();//获取当前节点的下一级所有子元素节点
for(Element ele : eles) {
System.out.println(ele.getName()+"=="+ele.getText());
}
} catch(Exception e) {
throw new RuntimeException(e);
}
}
}