class ReadXML extends Thread { public void run() { try { //Open http connection HttpConnection httpConnection = (HttpConnection) Connector.open(URL); //Initilialize XML parser KXmlParser parser = new KXmlParser(); parser.setInput(new InputStreamReader(httpConnection.openInputStream())); parser.nextTag(); parser.require(XmlPullParser.START_TAG, null, "catalog"); //Iterate through our XML file while (parser.nextTag () != XmlPullParser.END_TAG) readXMLData(parser); parser.require(XmlPullParser.END_TAG, null, "catalog"); parser.next(); parser.require(XmlPullParser.END_DOCUMENT, null, null); } catch (Exception e) { e.printStackTrace (); resultItem.setLabel ("Error:"); resultItem.setText (e.toString ()); } } } private void readXMLData(KXmlParser parser) throws IOException, XmlPullParserException { //Parse our XML file parser.require(XmlPullParser.START_TAG, null, "title"); Book book = new Book(); while (parser.nextTag() != XmlPullParser.END_TAG) { parser.require(XmlPullParser.START_TAG, null, null); String name = parser.getName(); String text = parser.nextText(); System.out.println ("<"+name+">"+text); if (name.equals("name")) book.setName(text); else if (name.equals("description")) book.setDescription(text); else if (name.equals("author")) book.setAuthor(text); else if (name.equals("rating")) book.setRating(text); else if (name.equals("available")) book.setAvailable(text); parser.require(XmlPullParser.END_TAG, null, name); } bookVector.addElement(book); parser.require(XmlPullParser.END_TAG, null, "title"); } 对应的XML文件 <?xml version="1.0"?> <catalog> <title> <name>EJB 2</name> <description>EJB 2 Fundamentals</description> <author>Jason</author> <rating>4</rating> <available>Yes</available> </title> <title> <name>Applied XML</name> <description>Advanced XML Parsing Programming</description> <author>Jason</author> <rating>5</rating> <available>Yes</available> </title> </catalog>