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

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

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

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

Michael Juntao Yuan

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

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

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



15.5 Amazon Services via XmlPull


Using XmlPull, we parse the document linearly. The application has to remember the state information to retrieve XML contents based on the context. The code is listed in Listings 15.4 and 15.5. The logic flow is the following:


When method getBooksViaPull() encounters a Details start tag, it passes the parser control to method getBookDetailsViaPull().

Method getBookDetailsViaPull() instantiates a new BookDetails object and stores the value of the url attribute in it.

When method getBookDetailsViaPull() encounters ProducteName, Authors, OurPrice, and UsedPrice tags, it stores their text values into appropriate fields in the BookDetails object.

At the Details close tag, method getBookDetailsViaPull() returns the populated BookDetails object. Method getBooksViaPull() stores the BookDetails object into a Vector Books and moves forward to the next Details start tag.


After the parsing is done, all useful data is extracted and stored in the Books Vector.

Listing 15.4. The AmazonLite.getBooksViaPull() method


Vector getBooksViaPull (InputStream is) throws Exception {
Vector books = new Vector ();
InputStreamReader reader = new InputStreamReader(is);
KXmlParser parser = new KXmlParser();
parser.setInput(reader);
int eventType = parser.getEventType();
while (eventType != parser.END_DOCUMENT) {
// Only respond to the <Details> start tag
if (eventType == parser.START_TAG) {
if ( parser.getName().equals("Details") ) {
BookDetails bd = getBookDetailsViaPull(parser);
books.addElement( bd );
}
}
eventType = parser.next();
}
return books;
}

Listing 15.5. The AmazonLite.getBookDetailsViaPull() method


BookDetails getBookDetailsViaPull (XmlPullParser parser)
throws Exception {
BookDetails bd = new BookDetails ();
// get attribute value from the <Details>
// start tag
bd.url = parser.getAttributeValue(null, "url");
int eventType = parser.next();
while ( true ) {
// Break out the loop at </Details> end tag
if ( eventType == parser.END_TAG ) {
if ( parser.getName().equals("Details") ) {
break;
}
}
if ( eventType == parser.START_TAG ) {
String tagname = parser.getName();
if ( tagname.equals("ProductName") ) {
// Proceed to the enclosed Text node
parser.next();
bd.title = parser.getText().trim();
}
if ( tagname.equals("Authors") ) {
// First <Author> start tag
parser.next();
// White space between tags
parser.next();
// Proceed to the enclosed Text node
parser.next();
bd.firstAuthor = parser.getText().trim();
}
if ( tagname.equals("OurPrice") ) {
// Proceed to the enclosed Text node
parser.next();
bd.newPrice = parser.getText().trim();
}
if ( tagname.equals("UsedPrice") ) {
// Proceed to the enclosed Text node
parser.next();
bd.usedPrice = parser.getText().trim();
}
}
eventType = parser.next();
}
return bd;
}

Note

In this particular example, all we do is extract information from XML document into a string for display. For this purpose, we do not need intermediate objects such as Books and BookDetails. The extra storage objects are introduced to illustrate how the parser is used in general-purpose applications.


/ 204