开发者

Parsing of this string

开发者 https://www.devze.com 2023-01-17 00:43 出处:网络
I am working on the Parsing of the response from a web Service: \"http://www.google.com/ig/api?weather=Ahmedabad\"

I am working on the Parsing of the response from a web Service: "http://www.google.com/ig/api?weather=Ahmedabad"

Now I am going parallel with an parsing example available on the Internet, This is my Response :

<?xml version="1.0" ?> 
- <xml_api_reply version="1"> 
- <current_conditions>
  <condition data="Haze" /> 
  <temp_f data="84" /> 
  <temp_c data="29" /> 
  <humidity data="Humidity: 74%" /> 
  <icon data="/ig/images/wea开发者_JS百科ther/haze.gif" /> 
  <wind_condition data="Wind: NW at 13 mph" /> 
- </current_conditions> 

Can anybody help me out how create objects of the Outer tags if necessary?

Sorry if I am asking a stupid Question. Thanks, david


Here you have an article titled Working with XML on Android, which looks to be exactly what you need.


Use the SAX parser and the method parse(InputSource is, DefaultHandler dh). Write your own class which extends DefaultHandler. The parsing logic is in the handler.

Values between tags
Use the method characters(char[] ch, int start, int length) to store the characters in between the xml tags in a temporary variable. Something like "tempValue.append(char, start, length);" will do it.
In the endElement(String uri, String localName, String qName) method you can then save the temp value when you know which localName, i.e "tag name", it has.

Attribute values
startElement(String uri, String localName, String qName, Attributes attributes) method makes it possible to read the attributes values within a tag. For example < condition data="Haze" /> contains the value "Haze" which comes with the attribute "data" when the localName is condition. In this case try "attributes.getValue("data");"

Good Luck

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号