Enterprise J2ME Developing Mobile Java Applications [Electronic resources] نسخه متنی

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

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

Enterprise J2ME Developing Mobile Java Applications [Electronic resources] - نسخه متنی

Michael Juntao Yuan

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

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

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



15.3 XML Parsing Models


The XML parser converts text-based XML documents to memory objects accessible to computer programs. There are several ways to parse an XML document.


15.3.1 SAX


SAX (Simple API for XML) is an event-based parsing model. The parser goes through the entire document in a linear pass. When the parser encounters an XML entitysuch as a tag, a piece of enclosed text, or an attributeit emits an event. The events are captured and processed by an event-handler method. The application developer implements the event handler to do application-specific tasks with those events. The kXML v1.2 supports SAX. Figure 15.1 illustrates the SAX parsing process.


Figure 15.1. SAX parsing process.


A simple SAX application looks like the following (Listing 15.2).

Listing 15.2. A simple SAX program



// Create a SAX parser.
// This is implementation specific.
Parser p = new SAXParser ();
// "handler" is the callback object that processes SAX events.
p.setDocumentHandler( handler );
p.parse();

Although SAX is simple to implement and very popular, it is outdated by the newer pull-based parsing APIs. For more examples using SAX, please refer to Section 17.2.


15.3.2 XMLPull


One big problem the SAX model has is that it is push based: Once the parsing is started, parsing events are pushed in continuously. The parser runs through the entire XML document in one pass. Developers have no control over the flow of the parsing process. For example, let's suppose that you are looking for a specific piece of information located in the middle of an XML document. Under the SAX model, you cannot stop parsing after you retrieve the data. The parse keeps going until it finishes the entire document. This is ineffective, especially for mobile clients.

XmlPull API gives developers more control over the parsing flow. Since the parser is pull based, the application can pause the parsing to take care of other things and come back later, or it can even stop the parsing before the end of the document is researched. The kXML v2.0 supports the XmlPull v1.0 API. Figure 15.2 illustrates the XMLPull parsing process.


Figure 15.2. XMLPull parsing process.


The heart of the XmlPull API is the XmlPullParser interface. XmlPull providers supply their own XmlPullParser implementation through the XmlPullParserFactory factory class. XmlPullParser defines a number of event types (e.g., the START_TAG event) and data access methods (e.g., the getAttributeValue() method). Core methods to control the parsing flow are next() and nextToken().

The next() method advances the parser to the next event. Event types seen by the next() method are START_TAG, TEXT, END_TAG, and END_DOCUMENT.

The nextToken() method gives developers a finer control. It sees all the events the next() method sees. In addition, it sees and reports the following events: COMMENT, CDSECT, DOCDECL, ENTITY_REF, PROCESSING_INSTRUCTION, and IGNORABLE_WHITESPACE.


The use of an XmlPull parser is illustrated in Section 15.5.


15.3.3 Document Model


Both SAX and XmlPull treats the hierarchical XML data structure as a linear flow. To reconstruct data into a logical tree structure requires the developer to control the event handler carefully with flags and case statements. This kind of code is hard to write and hard to debug. Also, SAX and XmlPull support only serial access. We do not have random access to any node in the document. The document model parsers come to the rescue.

A document model parser is essentially a SAX or XmlPull parser with a predefined event handler that stores XML information into an in-memory tree. The application can then access and manipulate any data in the tree model using a set of API methods. Figure 15.3 illustrates the DOM (Document Object Model) parsing process.


Figure 15.3. DOM parsing.


The building block of any document model is the Node object. The Node class defines methods that allows multiple Node objects to be linked into a tree structure. The XML document is represented by such a tree. Objects representing other XML entities, such as elements, attributes, and text, inherit from Node.

A standard XML document model API is DOM, which is defined by the W3C. However, the standard DOM is quite complex. There are several easy-to-use DOM-like APIs, such as the JDOM API, in the Java world. For mobile devices, full support for DOM has proven too expensive. A particularly interesting lightweight XML object model is kDOM, supported by both kXML v1.2 and v2.0. Code examples for kDOM are given in Section 15.6.


/ 204