Lightweight XML Parser: KXML

xiaoxiao2021-03-06  65

KXML IS A SMALL XML PULL PARSER, Specially Designed for Constrained Environments Such As Applets, Personal Java or MIDP Devices.

The smallest version is only 11K, which is much smaller than those huge XML parsing. You can use it when you don't need to quaint XML analysis.

Here is a sample code using KXML:

Import org.xmlpull.v1. *;

Import java.util. *;

Import java.io. *;

Import java.net. *;

/ **

* A Simple Example Illustrationg Some Difference of The XMLPull API

* And Sax. for the corresponding sax based impLementation, please refer to

* http://www.cafeconleche.org/slides/SD2001EAST/Xmlandjava/81.html ff. * /

Public class weblogs {

Static List Listchannels ()

THROWS IOEXCEPTION, XMLPullParsexception {

Return ListChannels ("http://static.userland.com/weblogmonitor/logs.xml");

}

Static List Listchannels (String Uri)

THROWS IOEXCEPTION, XMLPullParsexception {

Vector result = new vector ();

InputStream IS = New URL (URI) .openStream ();

XMLPullParser Parser =

XmlpullParserfactory.newinstance (). NewpullParser ();

Parser.setInput (IS, NULL);

Parser.nexttag ();

Parser.Require (XmlpullParser.Start_tag, "", "WebLogs");

While (Parser.nextTag () == XmlpullParser.Start_tag) {

String Url = Readsingle (PARSER);

IF (URL! = NULL)

Result.AddeElement (URL);

}

Parser.Require (XmlpullParser.end_tag, "", "WebLogs");

PARSER.NEXT ();

Parser.Require (XmlpullParser.end_Document, null, null);

Is.close ();

Parser.setInput (null);

Return Result;

}

Public Static String Readsingle (XMLPullParser Parser)

THROWS IOEXCEPTION, XMLPullParsexception {

String Url = NULL;

Parser.Require (XmlpullParser.Start_tag, "", "log");

While (Parser.nextTag () == XmlpullParser.Start_tag) {

String name = parse.getname ();

String content = parser.nextText (); if (Name.Equals ("URL"))

URL = Content;

Parser.Require (XmlpullParser.end_tag, "", name);

}

Parser.Require (XmlpullParser.end_TAG, "", "LOG");

Return URL;

}

Public static void main (string [] args)

THROWS IOEXCEPTION, XMLPullParsexception {

List urls =

Args.length> 0

? Listchannels (args [0])

: ListChannels ();

For (Iterator I = Urls.Itemrator (); I.hasNext ();)

System.out.println (i.next ());

}

}

转载请注明原文地址:https://www.9cbs.com/read-87308.html

New Post(0)