1. Import kxml.zip (kxml 1.21 stable version) to your project resources first)
Http://kxml.objectweb.org/software/downloads/
2. Then you create an httpconnection to send a URL request and return in
3, the final parsing in content, if contains Chinese content, pay attention to the need to set the read byte format for UTF-8, otherwise there will be garbled.
For example, if you are looking for a hotel from Google CN, You need to obtain its name, address, phone number, and coordinate information. The output format is kml, And the URL request is
Http://ditu.google.cn/maps? Output = kml & HL = ZH-CN & Server Load balancer = 31.2309321, 121.4755366 & START = 0 & num = 10 & Q = Hotel
The code snippet for parsing the returned kml information is as follows:
Public void parse (inputstream in) throws ioexception {
System. Out. println ("Enter parse: In ");
Reader reader = new inputstreamreader (in, "UTF-8 ");
Xmlparser parser = new xmlparser (Reader );
Parseevent Pe = NULL;
Parser. Skip ();
Parser. Read (XML. start_tag, null, "kml ");
Parser. Skip ();
Parser. Read (XML. start_tag, null, "folder ");
Boolean trucking = true;
// Boolean first = true;
While (trucking ){
Pe = parser. Read ();
If (PE. GetType () = xml. start_tag ){
String name = PE. getname ();
If (name. Equals ("placemark ")){
String title, address, coordinates, snippet, Tel;
Title = address = coordinates = snippet = Tel = NULL;
While (PE. GetType ()! = Xml. end_tag) |
(PE. getname (). Equals (name) = false )){
Pe = parser. Read ();
If (PE. GetType () = xml. start_tag &&
PE. getname (). Equals ("name ")){
Pe = parser. Read ();
Title = PE. gettext ();
}
Else if (PE. GetType () = xml. start_tag &&
PE. getname (). Equals ("coordinates ")){
Pe = parser. Read ();
Coordinates = PE. gettext ();
}
Else if (PE. GetType () = xml. start_tag &&
PE. getname (). Equals ("snippet ")){
Pe = parser. Read ();
Snippet = PE. gettext ();
// The description is in the format of "address <br/> Tel no"
Int beg = snippet. indexof ("<");
Int end = snippet. indexof ("> ");
Address = snippet. substring (0, beg );
Tel = snippet. substring (end, snippet. Length ());
}
}
// Here, listener is an abstract interface for processing XML parsing results. It is usually implemented by form objects that are interested in XML.
Mresultlistener. itemparsed (title, address, coordinates, TEL );
}
Else {
While (PE. GetType ()! = Xml. end_tag) |
(PE. getname (). Equals (name) = false ))
Pe = parser. Read ();
}
}
If (PE. GetType () = xml. end_tag &&
PE. getname (). Equals ("kml "))
Trucking = false;
}
}