Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is StAX in JDK6.0

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "what is StAX in JDK6.0", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what is StAX in JDK6.0" this article.

StAX (JSR 173) is another API for dealing with XML documents besides DOM and SAX in JDK6.0.

The origin of StAX

There are two ways to process XML documents in JAXP1.3 (JSR 206): DOM (Document Object Model) and SAX (Simple API for XML). Since both JAXB2 (JSR 222) and JAX-WS 2.0 (JSR 224) in JDK6.0 will be used in StAX, Sun decided to add StAX to the JAXP family and upgrade the version of JAXP to 1.4 (JAXP1.4 is the maintenance version of JAXP1.3). The version of JAXP in JDK6 is 1. 4.

Introduction to StAX

StAX is an acronym for The Streaming API for XML, an API.StAX that uses pull schema to parse (pull-parsing) XML documents by providing an API based on event iterator (Iterator) to allow programmers to control the parsing process of xml documents. The program traverses the event iterator to handle every parsing event, which can be seen as pulled by the program, that is, the program causes the parser to generate a parsing event and then handle it. It then causes the parser to generate the next parsing event, which loops until the document Terminator is reached SAX is also based on events to deal with xml documents, but is parsed with push mode, parsing events are generated after the parser parses the entire xml document, and then pushed to the program to handle these events; DOM's approach is to map the entire xml document to a memory tree, so that you can easily get data from parent and child nodes as well as sibling nodes, but if the document is large, it will seriously affect performance. The following is a comparison of these API (reproduced from http://www.blogjava.net/hsith/archive/2006/06/29/55817.html)

XML Parser API Feature Summary

Feature

StAX

SAX

DOM

TrAX

API Type

Pull, streaming

Push, streaming

In memory tree

XSLT Rule

Ease of Use

High

Medium

High

Medium

XPath Capability

No

No

Yes

Yes

CPU and Memory Efficiency

Good

Good

Varies

Varies

Forward Only

Yes

Yes

No

No

Read XML

Yes

Yes

Yes

Yes

Write XML

Yes

No

Yes

Yes

Create, Read, Update, Delete

No

No

Yes

No

StAX code demo

The following code demonstrates how to read an xml document and generate an xml document through StAX

Public class StaxTester {

Public static void main (String [] args) throws XMLStreamException, FileNotFoundException {

ReadXMLByStAX (); / / parsing xml documents with XMLEventReader

WriteXMLByStAX (); / / write xml documents with XMLStreamWriter

}

Private static void readXMLByStAX () throws XMLStreamException, FileNotFoundException {

XMLInputFactory xmlif = XMLInputFactory.newInstance ()

XMLEventReader xmler = xmlif.createXMLEventReader (StaxTester.class.getResourceAsStream ("test.xml"))

XMLEvent event

StringBuffer parsingResult = new StringBuffer ()

While (xmler.hasNext ()) {

Event = xmler.nextEvent ()

If (event.isStartElement ()) {/ / if the starting tag is parsed, StartElement se = event.asStartElement ()

ParsingResult.append ("")

} else if (event.isCharacters ()) {/ / if you are parsing text content

ParsingResult.append (event.asCharacters () .getData ())

} else if (event.isEndElement ()) {/ / if the closing tag is parsed

ParsingResult.append ("parsingResult.append (event.asEndElement (). GetName ()

ParsingResult.append (">")

}

}

System.out.println (parsingResult)

}

Private static void writeXMLByStAX () throws XMLStreamException, FileNotFoundException {

XMLOutputFactory xmlof = XMLOutputFactory.newInstance ()

XMLStreamWriter xmlw = xmlof.createXMLStreamWriter (new FileOutputStream ("output.xml"))

/ / write the default XML declaration to the xml document

Xmlw.writeStartDocument ()

Xmlw.writeCharacters ("n")

/ / write comments to the xml document

Xmlw.writeComment ("testing comment")

Xmlw.writeCharacters ("n")

/ / write a catalogs root element xmlw.writeStartElement ("catalogs")

Xmlw.writeNamespace ("myNS", "")

Xmlw.writeAttribute ("owner", "Chinajash")

Xmlw.writeCharacters ("n")

/ / write to the child element catalog

Xmlw.writeStartElement ("", "catalog")

Xmlw.writeAttribute ("id", "007")

Xmlw.writeCharacters ("Apparel")

/ / write the closing tag of the catalog element

Xmlw.writeEndElement ()

/ / write the closing tag of the catalogs element

Xmlw.writeEndElement ()

/ / end XML document xmlw.writeEndDocument ()

Xmlw.close ()

}

} http://blog.csdn.net/Chinajashhttp://blog.csdn.net/Chinajash

The test.xml file is as follows:

Book

Video

After running the above program, the console output is as follows:

Book

Video

After running the above program, the resulting output.xml file is as follows:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report