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代码
- public XMLStreamReader2 getStreamReader(String xmlStr) throws XMLStreamException {
- XMLInputFactory2 xmlif = (XMLInputFactory2) XMLInputFactory2
- .newInstance();
- xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES,
- Boolean.FALSE);
- xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,
- Boolean.FALSE);
- xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.FALSE);
- xmlif.configureForSpeed();
- XMLStreamReader2 xmlr = (XMLStreamReader2) xmlif.createXMLStreamReader(new BufferedReader(new StringReader(xmlStr)));
- return xmlr;
- }
- public XMLStreamReader2 getStreamReader(InputStream is) throws XMLStreamException, IOException {
- XMLInputFactory2 xmlif = (XMLInputFactory2) XMLInputFactory2
- .newInstance();
- xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES,
- Boolean.FALSE);
- xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,
- Boolean.FALSE);
- xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.FALSE);
- xmlif.configureForSpeed();
- XMLStreamReader2 xmlr = (XMLStreamReader2) xmlif.createXMLStreamReader(new BufferedReader(new InputStreamReader(is, "UTF-8")));
- return xmlr;
- }
- XMLStreamReader2 xmlsr = null;
- try {
- xmlsr = this.getStreamReader(str);
- int eventType = xmlsr.getEventType();
- list = new ArrayList<OfcardMainclass>();
- // 包装大类数据
- OfcardMainclass classof = null;
- while (xmlsr.hasNext()) {
- eventType = xmlsr.next();
- switch (eventType) {
- case XMLEvent2.START_ELEMENT:
- String name = xmlsr.getName().getLocalPart();
- if (name.equals("aa"))
- String s1 = xmlsr.getElementText();
- if (name.equals("bb"))
- String s2 = xmlsr.getAttributeValue(null, "att"));
- break;
- case XMLEvent2.END_ELEMENT:
- if (xmlsr.getName().getLocalPart().equals(
- "aa"))
- break;
- }
- }
- } finally {
- if (xmlsr != null)
- xmlsr.close();
- }
时间: 2024-10-25 21:36:56