Sample Code for parsing Chinese XML using j2_kxml

Source: Internet
Author: User

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;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.