xml解析技术性能对比

转自:http://blog.csdn.net/zgf19930504/article/details/49506567

  java 解析XML 的方法有很多, 常见的解析技术有 SAX 解析, DOM 解析, JDOM 解析, DOM4J 解析, JAXB解析等,其中SAX 解析采用的是流式解析,一遍过,不能折回解析,占用内存少; 而DOM ,JDOM,DOM4J,JAXB 解析采用的是将整个XML 文档全部加载到内存中,然后进行解析,此种解析方式占用内存大,解析效率相对较慢。 接下来笔者就简单地做一下性能对比分析。

【1. 对比SAX、DOM、JDOM、DOM4J、JAXB 在解析XML 方面的速度对比】

【students_bigfile.xml 格式, 大小82.6M 】

 

[html]  view plain  copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <Students>  
  4.     <!--这是第1个Student 元素-->  
  5.     <Student grade="2" index="1">  
  6.         <Name>zong_0</Name>  
  7.         <Age>20</Age>  
  8.         <Sex>boy</Sex>  
  9.         <Address>beijing No.0</Address>  
  10.         <Number>1000</Number>  
  11.     </Student>  
  12.     <!--这是第2个Student 元素-->  
  13.     <Student grade="1" index="2">  
  14.         <Name>zong_1</Name>  
  15.         <Age>21</Age>  
  16.         <Sex>girl</Sex>  
  17.         <Address>beijing No.1</Address>  
  18.         <Number>1001</Number>  
  19.     </Student>  
  20.     <!--这是第3个Student 元素-->  
  21.     <Student grade="2" index="3">  
  22.         <Name>zong_2</Name>  
  23.         <Age>22</Age>  
  24.         <Sex>boy</Sex>  
  25.         <Address>beijing No.2</Address>  
  26.         <Number>1002</Number>  
  27.     </Student>  
  28.     <!-- 省略, 共50 万个Student 片段    -->  
  29. </Students>  


【Student 类】由于涉及到JAXB 解析,所以用xjc 反转出的Student 类。

 

 

[java]  view plain  copy
 
  1. //  
  2. // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2   
  3. // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>   
  4. // Any modifications to this file will be lost upon recompilation of the source schema.   
  5. // Generated on: 2015.10.29 at 01:04:05 PM CST   
  6. //  
  7.   
  8. package org.zgf.xml.jaxb.bean;  
  9.   
  10. import javax.xml.bind.annotation.XmlAccessType;  
  11. import javax.xml.bind.annotation.XmlAccessorType;  
  12. import javax.xml.bind.annotation.XmlAttribute;  
  13. import javax.xml.bind.annotation.XmlElement;  
  14. import javax.xml.bind.annotation.XmlRootElement;  
  15. import javax.xml.bind.annotation.XmlType;  
  16.   
  17. /** 
  18.  * <p> 
  19.  * Java class for anonymous complex type. 
  20.  *  
  21.  * <p> 
  22.  * The following schema fragment specifies the expected content contained within 
  23.  * this class. 
  24.  *  
  25.  * <pre> 
  26.  * <complexType> 
  27.  *   <complexContent> 
  28.  *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
  29.  *       <sequence> 
  30.  *         <element ref="{}Name"/> 
  31.  *         <element ref="{}Age"/> 
  32.  *         <element ref="{}Sex"/> 
  33.  *         <element ref="{}Number"/> 
  34.  *         <element ref="{}Address"/> 
  35.  *       </sequence> 
  36.  *       <attribute name="index" type="{http://www.w3.org/2001/XMLSchema}string" /> 
  37.  *       <attribute name="grade" type="{http://www.w3.org/2001/XMLSchema}string" /> 
  38.  *     </restriction> 
  39.  *   </complexContent> 
  40.  * </complexType> 
  41.  * </pre> 
  42.  *  
  43.  *  
  44.  */  
  45. @XmlAccessorType(XmlAccessType.FIELD)  
  46. @XmlType(name = "", propOrder = { "name""age""sex""number""address" })  
  47. @XmlRootElement(name = "Student")  
  48. public class Student {  
  49.   
  50.     @XmlElement(name = "Name", required = true)  
  51.     protected String name;  
  52.     @XmlElement(name = "Age", required = true)  
  53.     protected String age;  
  54.     @XmlElement(name = "Sex", required = true)  
  55.     protected String sex;  
  56.     @XmlElement(name = "Number", required = true)  
  57.     protected String number;  
  58.     @XmlElement(name = "Address", required = true)  
  59.     protected String address;  
  60.     @XmlAttribute(name = "index")  
  61.     protected String index;  
  62.     @XmlAttribute(name = "grade")  
  63.     protected String grade;  
  64.   
  65.     /** 
  66.      * Gets the value of the name property. 
  67.      *  
  68.      * @return possible object is {@link String } 
  69.      *  
  70.      */  
  71.     public String getName() {  
  72.         return name;  
  73.     }  
  74.   
  75.     /** 
  76.      * Sets the value of the name property. 
  77.      *  
  78.      * @param value 
  79.      *            allowed object is {@link String } 
  80.      *  
  81.      */  
  82.     public void setName(String value) {  
  83.         this.name = value;  
  84.     }  
  85.   
  86.     /** 
  87.      * Gets the value of the age property. 
  88.      *  
  89.      * @return possible object is {@link String } 
  90.      *  
  91.      */  
  92.     public String getAge() {  
  93.         return age;  
  94.     }  
  95.   
  96.     /** 
  97.      * Sets the value of the age property. 
  98.      *  
  99.      * @param value 
  100.      *            allowed object is {@link String } 
  101.      *  
  102.      */  
  103.     public void setAge(String value) {  
  104.         this.age = value;  
  105.     }  
  106.   
  107.     /** 
  108.      * Gets the value of the sex property. 
  109.      *  
  110.      * @return possible object is {@link String } 
  111.      *  
  112.      */  
  113.     public String getSex() {  
  114.         return sex;  
  115.     }  
  116.   
  117.     /** 
  118.      * Sets the value of the sex property. 
  119.      *  
  120.      * @param value 
  121.      *            allowed object is {@link String } 
  122.      *  
  123.      */  
  124.     public void setSex(String value) {  
  125.         this.sex = value;  
  126.     }  
  127.   
  128.     /** 
  129.      * Gets the value of the number property. 
  130.      *  
  131.      * @return possible object is {@link String } 
  132.      *  
  133.      */  
  134.     public String getNumber() {  
  135.         return number;  
  136.     }  
  137.   
  138.     /** 
  139.      * Sets the value of the number property. 
  140.      *  
  141.      * @param value 
  142.      *            allowed object is {@link String } 
  143.      *  
  144.      */  
  145.     public void setNumber(String value) {  
  146.         this.number = value;  
  147.     }  
  148.   
  149.     /** 
  150.      * Gets the value of the address property. 
  151.      *  
  152.      * @return possible object is {@link String } 
  153.      *  
  154.      */  
  155.     public String getAddress() {  
  156.         return address;  
  157.     }  
  158.   
  159.     /** 
  160.      * Sets the value of the address property. 
  161.      *  
  162.      * @param value 
  163.      *            allowed object is {@link String } 
  164.      *  
  165.      */  
  166.     public void setAddress(String value) {  
  167.         this.address = value;  
  168.     }  
  169.   
  170.     /** 
  171.      * Gets the value of the index property. 
  172.      *  
  173.      * @return possible object is {@link String } 
  174.      *  
  175.      */  
  176.     public String getIndex() {  
  177.         return index;  
  178.     }  
  179.   
  180.     /** 
  181.      * Sets the value of the index property. 
  182.      *  
  183.      * @param value 
  184.      *            allowed object is {@link String } 
  185.      *  
  186.      */  
  187.     public void setIndex(String value) {  
  188.         this.index = value;  
  189.     }  
  190.   
  191.     /** 
  192.      * Gets the value of the grade property. 
  193.      *  
  194.      * @return possible object is {@link String } 
  195.      *  
  196.      */  
  197.     public String getGrade() {  
  198.         return grade;  
  199.     }  
  200.   
  201.     /** 
  202.      * Sets the value of the grade property. 
  203.      *  
  204.      * @param value 
  205.      *            allowed object is {@link String } 
  206.      *  
  207.      */  
  208.     public void setGrade(String value) {  
  209.         this.grade = value;  
  210.     }  
  211.   
  212.     @Override  
  213.     public String toString() {  
  214.         return "Student [name=" + name + ", age=" + age + ", sex=" + sex + ", number=" + number + ", address=" + address + ", index=" + index + ", grade=" + grade + "]";  
  215.     }  
  216.   
  217. }  


【解析一个 82.6M 的xml 文档,所消耗的时间】

扫描二维码关注公众号,回复: 430429 查看本文章

 

【所消耗的内存占用比】

 

【为了防止同一个测试用例中,不同的解析器占用内存相互影响,笔者将不同的解析器分为单独的测试用例进行测试,测试的xml 文档依然是这个xml 文档】【SAX(170M) < DOM4J(410M) < JAXB(690M) < JDOM(750M) < DOM(950M);

【1. SAX 解析】

【2. DOM 解析】

【3. JDOM 解析】

【4. DOM4J 解析】

【5. JAXB 解析】

 

【综合对比分析】

1. 解析速度对比:SAX > DOM4J > JAXB > JDOM > DOM

2. 解析内存对比: SAX < DOM4J < JAXB < JDOM < DOM

3. 编程复杂对比:SAX > DOM > JDOM > DOM4J > JAXB

 

综上所述,笔者推荐使用JAXB,DOM4J,SAX解析三种技术:

SAX: 解析速度最快,占用内存最小,编程难度大,处理业务逻辑比较复杂。

DOM4J:解析速度较,占用内存较大,编程较简单,处理业务逻辑稍简单。

JAXB: 解析速度稍慢,占用内存较大,编程最简单,处理语无逻辑最简单。

 

【注】

1. 项目源代码下载地址:下载

2. 项目示例运行时,需要调整JVM 内存,方法参见:《修改jvm 虚拟机内存方法》

3. JVM 内存监控,方法参见: 《jvm 内存监控工具》

猜你喜欢

转载自xiaoxiaoher.iteye.com/blog/2405202