Java-XML解析第一篇主流开源类库解析XML
1、流行的XML解析框架
1》底层解析方式:存在编码复杂性、难扩展、难复用....。想了解底层解析方式请参考:浅谈 Java XML 底层解析方式
2》Dom4j:基于 JAXP 解析方式,性能优异、功能强大、极易使用的优秀框架。
3》Jdom:本质也是基于 JAXP 但包结构被重新组织, API 大量使用了 Collections 类,在性能上被 dm4j 压了好几个档次。
4》XStream:基于 xmlpull 的 OXMapping 技术,更加倾向于将 XML 解析后映射为 Java 世界中的对象。
2、建立一个简单的demo.xml文件,用于提供给各种方式解析
<?xml version="1.0"?> <classGrid> <classGridlb> <class_id>320170105000009363</class_id> <class_number>0301</class_number> <adviser>018574</adviser> <studentGrid> <studentGridlb> <stu_id>030101</stu_id> <stu_name>齐天</stu_name> <stu_age>9</stu_age> <stu_birthday>2008-11-07</stu_birthday> </studentGridlb> <studentGridlb> <stu_id>030102</stu_id> <stu_name>张惠</stu_name> <stu_age>10</stu_age> <stu_birthday>2009-04-08</stu_birthday> </studentGridlb> <studentGridlb> <stu_id>030103</stu_id> <stu_name>龙五</stu_name> <stu_age>9</stu_age> <stu_birthday>2008-11-01</stu_birthday> </studentGridlb> </studentGrid> </classGridlb> <classGridlb> <class_id>420170105000007363</class_id> <class_number>0302</class_number> <adviser>018577</adviser> <studentGrid> <studentGridlb> <stu_id>030201</stu_id> <stu_name>马宝</stu_name> <stu_age>10</stu_age> <stu_birthday>2009-09-02</stu_birthday> </studentGridlb> </studentGrid> </classGridlb> </classGrid>
3、Dom4j、Jdom、Xstream不同的解析方式
1》Dom4j方式,性能好,API灵活,支持对 XML 文档的增删改查动作,毕竟是以树模型加载到内存中进行的操作。有可能成为将来的主流。项目结构如下:
pom.xml
<dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> <exclusions> <exclusion> <groupId>xml-apis</groupId> <artifactId>xml-apis</artifactId> </exclusion> </exclusions> </dependency>
剔除 xml-apis 的用意 JDK 中已经有对应的类。
App.java
package com.lfy.cn.javaXmlPasareTest; import java.io.File; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; /** * * */ public class App { public static void main( String[] args ) throws DocumentException, ParseException{ String path = System.getProperty("user.dir")+"\\src\\demo.xml"; SAXReader reader = new SAXReader(); Document document = reader.read(new File(path)); List<StudentGridlb> studentGridlbList = new ArrayList<>(); StudentGridlb studentGridlbVo; for (Object classGridlb : document.getRootElement().elements("classGridlb")) { Element classGridlbEle = (Element) classGridlb; DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); for (Object studentGridlb : classGridlbEle.element("studentGrid").elements("studentGridlb")) { Element studentGridlbEle = (Element) studentGridlb; studentGridlbVo = new StudentGridlb(); studentGridlbVo.setStu_id(studentGridlbEle.elementTextTrim("stu_id")); studentGridlbVo.setStu_age(Integer.parseInt(studentGridlbEle.elementTextTrim("stu_age"))); studentGridlbVo.setStu_name(studentGridlbEle.elementTextTrim("stu_name")); studentGridlbVo.setStu_birthday(format.parse(studentGridlbEle.elementTextTrim("stu_birthday"))); studentGridlbList.add(studentGridlbVo); } } System.out.println(studentGridlbList); } }
运行结果:
2》Jdom,基于树处理 XML,需要将树加载到内存中,所以解析大文件对内存的要求比较高。支持对 XML 文档的增删改查动作,毕竟是以树模型加载到内存中进行的操作。解析速度还可以。
pom.xml
<dependency> <groupId>jdom</groupId> <artifactId>jdom</artifactId> <version>1.1</version> </dependency>
JdomTest.java
package com.lfy.cn.javaXmlPasareTest; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.jdom.output.XMLOutputter; /** * * */ public class JdomTest { public static void main( String[] args ) throws ParseException, FileNotFoundException, IOException, JDOMException{ String path = System.getProperty("user.dir")+"\\src\\demo.xml"; SAXBuilder jdomsaxBuilder = new SAXBuilder(false); Document doc = jdomsaxBuilder.build(path); Element rootElement = doc.getRootElement(); List<StudentGridlb> studentGridlbList = new ArrayList<>(); StudentGridlb studentGridlb; for (Object classGridlb : rootElement.getChildren("classGridlb")) { Element classGridlbEle = (Element) classGridlb; DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); for (Object studentGrid : classGridlbEle.getChild("studentGrid").getChildren("studentGridlb")) { Element studentGridEle = (Element) studentGrid; studentGridlb = new StudentGridlb(); studentGridlb.setStu_id(studentGridEle.getChildTextTrim("stu_id")); studentGridlb.setStu_age(Integer.parseInt(studentGridEle.getChildTextTrim("stu_age"))); studentGridlb.setStu_name(studentGridEle.getChildTextTrim("stu_name")); studentGridlb.setStu_birthday(format.parse(studentGridEle.getChildTextTrim("stu_birthday"))); studentGridlbList.add(studentGridlb); } } XMLOutputter outputter = new XMLOutputter(); outputter.output(doc, new FileOutputStream(path)); System.out.println(studentGridlbList); } }
3》Xstream:基于注解不需要其它辅助类或映射文件 的OXMapping 技术。可以将 JavaBean 序列化为 XML,或将 XML 反序列化为 JavaBean,使得XML序列化不再繁琐。也可以将 JavaBean 序列化成 Json 或反序列化,使用非常方便。没有映射文件而且底层使用 xmlpull 推模型解析 XML,高性能、低内存占用,结合简洁明了的 API,上手基本是分分钟的事情。