Perl Cd Bookshelf [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Perl Cd Bookshelf [Electronic resources] - نسخه متنی

Mark V. Scardina, Ben ChangandJinyu Wang

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید






Accessing XML with StAX

Pull parsing or StAX (Streaming API for XML) is a new Java standards effort to address some of the limitations of SAX parsing yet still maintain a streaming model. Defined under JSR-173 in the Java Community Process, XML Pull Parsing was designed with simplicity and performance in mind.

As in SAX parsing, StAX delivers events; however, these events are less granular and can be returned as objects. These events, listed next, are derived from XMLEvent:

StartDocument, EndDocument
StartElement, EndElement
Characters, CDATA, Comment
Processing Instruction
Entity Reference

A StAX parser has factories to create readers, writers, and events. They consist of the following:

javax.xml.stream.XMLInputFactory
javax.xml.stream.XMLOutputFactory
javax.xml.stream.XMLEventFactory

Each of these factory instances is obtained through the newInstance() static method. Specific implementations can be plugged into these interfaces as is done in the Oracle version.

As distinct from SAX, you don’t need to register handlers for every type of event that the parser will stream to you. Instead, once a reader in instantiated, you simply call the next() method on the reader to get the events. The following example shows this being done on the booklist.xml file:

XMLStreamReader reader =
XMLInputFactory.newInstance().createXMLStreamReader
(new FileInputStream("booklist.xml"));
while(reader.hasNext()) {
int eventType = reader.next();
if (eventType == XMLEvent.START_ELEMENT &&
reader.getLocalName().equals("\title")) {
reader.next();
System.out.println(reader.getText());
}
}

Note that the element to be searched for could be “pulled” while the rest is ignored. This is the cursor style of pull parsing and it provides fine-grained access to the document content.

There is a second style to StAX parsing that is more natural to Java programmers because it returns the events as objects with get and set type methods. The following is an example of this iterator style performing the same task:

XMLEventReader eventReader = 
XMLInputFactory.newInstance().createXMLEventReader
(new FileInputStream("booklist.xml"));
while(reader.hasNext()) {
XMLEvent event = reader.next();
if (event instanceof StartElement &&
((StartElement)event).getLocalName().equals("book\title"))
{
System.out.println( ((Characters)reader.next()).getData());
}
}

Note that the getLocalName() method had to be called on the event to pull the title, and the getData() method to extract the data to print it.

As you can see in these examples, it is the application, not the parser, that controls the process. This type of parsing is excellent for filtering XML, especially when dealing in small fragments.

The StAX writer can be used for limited transformation when a single pass is all that is necessary creating a subset or derivative XML document. However, the inability to traverse the document bidirectionally makes it a poor replacement for a DOM parser.

Pull parsing generally can be used wherever SAX parsing can be used—and in most cases with fewer lines of code. Pull parsing really has application in situations in which namespaces need to be supported. As in the DOM, there is the concept of scope to namespaces, where children inherit their parent’s namespace even when it is not explicitly declared. StAX supports retrieving this namespace information from elements, whereas SAX does not. Finally, the ability of StAX to support multiple data or input sources easily provides a uniform programming model that can produce more reliable XML processing in distributed environments. This reliability is due once again to the pull model, where a single parser can poll multiple data sources or inputs and merge their contents into a single context without the need for multiple threads. This is why StAX is the parsing method of choice for web service specifications such as JAX-RPC (Java API for XML-based RPC).

/ 218