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.         }  
  • 相关阅读:
    Quick-Cocos2d-x Lua脚本加密打包器
    Memcached保存sessionId
    windows操作系统重装后恢复svn仓库、tortoisesvn客户端信息、及权限信息的方法
    Fedora 17 开启ssh服务
    Spring mvc从一个controller到另一个controller
    FreeMarker设置编码格式
    又被数据坑了
    设置文件属性--C#点滴积累
    SQL点滴积累4--字符串替换功能Stuff()
    SQL点滴积累3--detete 删除的目标表数据与其他表有关联关系
  • 原文地址:https://www.cnblogs.com/guanghuiqq/p/6483457.html
Copyright © 2011-2022 走看看