Here is an example of what i need to get information from (XML):
Using xml Syntax Highlighting
- <ResultSet xsi:schemaLocation="urn:yahoo:lcl http://local.yahooapis.com/LocalSearchS ... sponse.xsd" totalResultsAvailable="32" totalResultsReturned="2" firstResultPosition="1">
- <ResultSetMapUrl>http://maps.yahoo.com/broadband/?tt=bank&tp=1</ResultSetMapUrl>
- −
- <Result id="17548186">
- <Title>US Bank</Title>
- <Address>1401 S Main St</Address>
- <City>Normal</City>
- <State>IL</State>
- <Phone>(309) 664-2846</Phone>
- <Latitude>40.496254</Latitude>
- <Longitude>-88.994449</Longitude>
- −
- <Rating>
- <AverageRating>5</AverageRating>
- <TotalRatings>1</TotalRatings>
- <TotalReviews>1</TotalReviews>
- <LastReviewDate>1165801320</LastReviewDate>
- −
- <LastReviewIntro>
- all employees that i have come in contact with here have been kind and most importantly: very helpful!!
- </LastReviewIntro>
- </Rating>
- <Distance>1.26</Distance>
- −
- <Url>
- http://local.yahoo.com/info-17548186-us-bank-normal
- </Url>
- −
- <ClickUrl>
- http://local.yahoo.com/info-17548186-us-bank-normal
- </ClickUrl>
- −
- <MapUrl>
- http://maps.yahoo.com/maps_result?q1=14 ... 1=17548186
- </MapUrl>
- <BusinessUrl>http://www.usbank.com/</BusinessUrl>
- <BusinessClickUrl>http://www.usbank.com/</BusinessClickUrl>
- −
- <Categories>
- <Category id="96925947">Banks</Category>
- </Categories>
- </Result>
- −
- <Result id="17544058">
- <Title>Commerce Bank ATM</Title>
- <Address>210 Broadway St</Address>
- <City>Normal</City>
- <State>IL</State>
- <Phone>(309) 823-7364</Phone>
- <Latitude>40.509374</Latitude>
- <Longitude>-88.985691</Longitude>
- −
- <Rating>
- <AverageRating>NaN</AverageRating>
- <TotalRatings>0</TotalRatings>
- <TotalReviews>0</TotalReviews>
- <LastReviewDate/>
- <LastReviewIntro/>
- </Rating>
- <Distance>0.42</Distance>
- −
- <Url>
- http://local.yahoo.com/info-17544058-co ... atm-normal
- </Url>
- −
- <ClickUrl>
- http://local.yahoo.com/info-17544058-co ... atm-normal
- </ClickUrl>
- −
- <MapUrl>
- http://maps.yahoo.com/maps_result?q1=21 ... 1=17544058
- </MapUrl>
- <BusinessUrl>http://www.commercebank.com/</BusinessUrl>
- <BusinessClickUrl>http://www.commercebank.com/</BusinessClickUrl>
- −
- <Categories>
- <Category id="96925947">Banks</Category>
- <Category id="96934297">B2B Banks</Category>
- </Categories>
- </Result>
- </ResultSet>
- −
- <!--
- ws03.ydn.ac4.yahoo.com compressed/chunked Tue Mar 23 19:57:56 PDT 2010
- -->
Parsed in 0.012 seconds, using GeSHi 1.0.8.4
Suppose I want to get the information in the <Title> tags and the <Distance> tags for each Result. I am only trying to get Title first then i will be able to figure out the Distance. Here Is what i have come up with based on a guide.:
Using java Syntax Highlighting
- package com.warriorpoint.androidxmlsimple;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.List;
- import com.warriorpoint.androidxmlsimple.Message;
- import android.sax.Element;
- import android.sax.EndElementListener;
- import android.sax.EndTextElementListener;
- import android.sax.RootElement;
- import android.util.Xml;
- public class BaseFeedParser {
- static String feedUrlString = "http://local.yahooapis.com/LocalSearchService/V3/localSearch?appid=YahooDemo" +
- "&query=bank&latitude=40.5141667&longitude=-88.9905556&radius=2&results=5";
- // names of the XML tags
- static final String RESULTSET = "ResultSet";
- static final String RESULT = "Result";
- static final String TITLE = "Title";
- static final String ADDRESS = "Address";
- private final URL feedUrl;
- protected BaseFeedParser(){
- try {
- this.feedUrl = new URL(feedUrlString);
- } catch (MalformedURLException e) {
- throw new RuntimeException(e);
- }
- }
- protected InputStream getInputStream() {
- try {
- return feedUrl.openConnection().getInputStream();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- public List<Message> parse() {
- final Message currentMessage = new Message();
- RootElement root = new RootElement(RESULTSET);
- final List<Message> messages = new ArrayList<Message>();
- Element itemlist = root.getChild(RESULT);
- itemlist.setEndElementListener(new EndElementListener(){
- public void end() {
- messages.add(currentMessage.copy());
- }
- });
- itemlist.getChild(TITLE).setEndTextElementListener(new EndTextElementListener(){
- public void end(String body) {
- currentMessage.setTitle(body);
- }
- });
- try {
- Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- return messages;
- }
- }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
Using java Syntax Highlighting
- package com.warriorpoint.androidxmlsimple;
- import java.util.Date;
- public class Message implements Comparable<Message>{
- private String title;
- private String address;
- private String distance;
- private String phone;
- public String getTitle() {
- return title;
- }
- public void setTitle(String Title) {
- this.title = title.trim();
- }
- // getters and setters omitted for brevity
- // public URL getLink() {
- // return link;
- // }
- //
- // public void setLink(String link) {
- // try {
- // this.link = new URL(link);
- // } catch (MalformedURLException e) {
- // throw new RuntimeException(e);
- // }
- // }
- public String getAddress() {
- return address;
- }
- public void setAddress(String Address) {
- this.address = Address.trim();
- }
- public String getPhone() {
- return phone;
- }
- public void setPhone(String Phone) {
- this.phone = Phone.trim();
- }
- public String getDistance() {
- return distance;
- }
- public void setDistance(String Distance) {
- this.distance = Distance.trim();
- }
- public Message copy(){
- Message copy = new Message();
- copy.title = title;
- return copy;
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append("Title: ");
- sb.append(title);
- return sb.toString();
- }
- // @Override
- // public int hashCode() {
- // final int prime = 31;
- // int result = 1;
- // result = prime * result + ((date == null) ? 0 : date.hashCode());
- // result = prime * result
- // + ((description == null) ? 0 : description.hashCode());
- // result = prime * result + ((link == null) ? 0 : link.hashCode());
- // result = prime * result + ((title == null) ? 0 : title.hashCode());
- // return result;
- // }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- Message other = (Message) obj;
- if (title == null) {
- if (other.title != null)
- return false;
- } else if (!title.equals(other.title))
- return false;
- return true;
- }
- @Override
- public int compareTo(Message arg0) {
- // TODO Auto-generated method stub
- return 0;
- }
- }
Parsed in 0.041 seconds, using GeSHi 1.0.8.4
Using java Syntax Highlighting
- package com.warriorpoint.androidxmlsimple;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.ListActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.ArrayAdapter;
- public class MessageList extends ListActivity {
- private List<Message> messages;
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- setContentView(R.layout.main);
- loadFeed();
- }
- private void loadFeed(){
- try{
- BaseFeedParser parser = new BaseFeedParser();
- messages = parser.parse();
- List<String> titles = new ArrayList<String>(messages.size());
- for (Message msg : messages){
- titles.add(msg.getTitle());
- }
- ArrayAdapter<String> adapter =
- new ArrayAdapter<String>(this, R.layout.row,titles);
- this.setListAdapter(adapter);
- } catch (Throwable t){
- Log.e("Business Locator",t.getMessage(),t);
- }
- }
- }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4
Using java Syntax Highlighting
- package com.warriorpoint.androidxmlsimple;
- import java.util.ArrayList;
- import java.util.List;
- import org.xml.sax.Attributes;
- import org.xml.sax.SAXException;
- import org.xml.sax.helpers.DefaultHandler;
- import static com.warriorpoint.androidxmlsimple.BaseFeedParser.*;
- public class RssHandler extends DefaultHandler{
- private List<Message> messages;
- private Message currentMessage;
- private StringBuilder builder;
- public List<Message> getMessages(){
- return this.messages;
- }
- @Override
- public void characters(char[] ch, int start, int length)
- throws SAXException {
- super.characters(ch, start, length);
- builder.append(ch, start, length);
- }
- @Override
- public void endElement(String uri, String localName, String name)
- throws SAXException {
- super.endElement(uri, localName, name);
- if (this.currentMessage != null){
- if (localName.equalsIgnoreCase("Title")){
- currentMessage.setTitle(builder.toString());
- // } else if (localName.equalsIgnoreCase(ADDRESS)){
- // currentMessage.setAddress(builder.toString());
- // } else if (localName.equalsIgnoreCase(PHONE)){
- // currentMessage.setPhone(builder.toString());
- // } else if (localName.equalsIgnoreCase(DISTANCE)){
- // currentMessage.setDistance(builder.toString());
- // } else if (localName.equalsIgnoreCase(ITEM)){
- // messages.add(currentMessage);
- }
- builder.setLength(0);
- }
- }
- @Override
- public void startDocument() throws SAXException {
- super.startDocument();
- messages = new ArrayList<Message>();
- builder = new StringBuilder();
- }
- @Override
- public void startElement(String uri, String localName, String name,
- Attributes attributes) throws SAXException {
- super.startElement(uri, localName, name, attributes);
- if (localName.equalsIgnoreCase("Title")){
- this.currentMessage = new Message();
- }
- }
- }
Parsed in 0.046 seconds, using GeSHi 1.0.8.4
I am unable to populate the ListView with any Results. Can someone please help me figure out what i am missing that could fix this problem?? Thanks for any help!
If i could just get help with Title then i should be able to figure the rest out.
MG




