zoukankan      html  css  js  c++  java
  • 基于Woodstox的StAX 2 (Streaming API for XML)解析XML

    StAX (Streaming API for XML)面向流的拉式解析XML,速度快、占用资源少,非常合适处理大数据量的xml文件。 

    详细教程和说明可以参见以下几篇文章: 

    使用 StAX 解析 XML,第 1 部分: Streaming API for XML (StAX) 简介 
    http://www.ibm.com/developerworks/cn/xml/x-stax1.html 

    使用 StAX 解析 XML,第 2 部分: 拉式解析和事件 
    http://www.ibm.com/developerworks/cn/xml/x-stax2.html 

    使用 StAX 解析 XML,第 3 部分: 使用定制事件和编写 XML 
    http://www.ibm.com/developerworks/cn/xml/x-stax3.html 

    Java6.0新特性之StAX--全面解析Java XML分析技术 
    http://zangweiren.iteye.com/blog/647334 

    Geronimo 叛逆者: 使用集成软件包:Codehaus 的 Woodstox 
    http://www.ibm.com/developerworks/cn/opensource/os-ag-renegade15/ 

    本文的目的是说明Woodstox包中的StAX2应用。 
    Woodstox官网http://woodstox.codehaus.org/ 

    下载woodstox-core.jar,核心包有两种开源协议apache的ASL和流行的LGPL,同时woodstox-core.jar需要stax2-api.jar的支持 

    stax2和stax有些不同,且对原来stax的代码不兼容 

    读取操作: 

    Java代码  收藏代码
      1. public XMLStreamReader2 getStreamReader(String xmlStr) throws XMLStreamException {  
      2.         XMLInputFactory2 xmlif = (XMLInputFactory2) XMLInputFactory2  
      3.                 .newInstance();  
      4.         xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES,  
      5.                 Boolean.FALSE);  
      6.         xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,  
      7.                 Boolean.FALSE);  
      8.         xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.FALSE);  
      9.         xmlif.configureForSpeed();  
      10.   
      11.         XMLStreamReader2 xmlr = (XMLStreamReader2) xmlif.createXMLStreamReader(new BufferedReader(new StringReader(xmlStr)));  
      12.           
      13.         return xmlr;  
      14.     }  
      15.       
      16.     public XMLStreamReader2 getStreamReader(InputStream is) throws XMLStreamException, IOException {  
      17.         XMLInputFactory2 xmlif = (XMLInputFactory2) XMLInputFactory2  
      18.                 .newInstance();  
      19.         xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES,  
      20.                 Boolean.FALSE);  
      21.         xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,  
      22.                 Boolean.FALSE);  
      23.         xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.FALSE);  
      24.         xmlif.configureForSpeed();  
      25.   
      26.         XMLStreamReader2 xmlr = (XMLStreamReader2) xmlif.createXMLStreamReader(new BufferedReader(new InputStreamReader(is, "UTF-8")));  
      27.   
      28.         return xmlr;  
      29.     }  
      30.   
      31. XMLStreamReader2 xmlsr = null;  
      32.   
      33. try {  
      34.             xmlsr = this.getStreamReader(str);  
      35.   
      36.             int eventType = xmlsr.getEventType();  
      37.   
      38.             list = new ArrayList<OfcardMainclass>();  
      39.             // 包装大类数据  
      40.             OfcardMainclass classof = null;  
      41.             while (xmlsr.hasNext()) {  
      42.                 eventType = xmlsr.next();  
      43.                 switch (eventType) {  
      44.                 case XMLEvent2.START_ELEMENT:  
      45.                     String name = xmlsr.getName().getLocalPart();  
      46.   
      47.                     if (name.equals("aa"))  
      48.                         String s1 = xmlsr.getElementText();  
      49.  if (name.equals("bb"))  
      50.                     String s2 = xmlsr.getAttributeValue(null, "att"));  
      51.                     break;  
      52.   
      53.                 case XMLEvent2.END_ELEMENT:  
      54.                     if (xmlsr.getName().getLocalPart().equals(  
      55.                             "aa"))  
      56.                     break;  
      57.                 }  
      58.             }  
      59.         } finally {  
      60.             if (xmlsr != null)  
      61.                 xmlsr.close();  
      62.         }  
  • 相关阅读:
    jQuery easyUI 的combogrid进行模糊匹配
    SQL 快速生成不重复的卡号
    用SQL语句查找包含有某个关键字的存储过程、触发器、函数等等
    C# DateTime日期格式化
    【SQL触发器】类型 FOR 、AFTER、 Instead of
    【BAT】批量给制定类型的文件名添加前缀
    【VBA】将Excel数据转化为txt文本数据
    【Python】将多个工作簿中的数据按列合并到同一个工作表
    【Python】从DB2数据库中取出字段信息并根据字段类型生成SQL
    【Oracle】多行数据合并为一行,使用逗号分隔
  • 原文地址:https://www.cnblogs.com/guanghuiqq/p/6483457.html
Copyright © 2011-2022 走看看