Parsing XML Help Me!

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Parsing XML Help Me!

Postby mmgill » Wed Mar 24, 2010 4:06 am

Hello. I was following a tutorial online to have get information from an XML file put into a ListView.
Here is an example of what i need to get information from (XML):

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <ResultSet xsi:schemaLocation="urn:yahoo:lcl http://local.yahooapis.com/LocalSearchS ... sponse.xsd" totalResultsAvailable="32" totalResultsReturned="2" firstResultPosition="1">
  2.  
  3. <ResultSetMapUrl>http://maps.yahoo.com/broadband/?tt=bank&tp=1</ResultSetMapUrl>
  4.  
  5.  
  6. <Result id="17548186">
  7.  
  8. <Title>US Bank</Title>
  9.  
  10. <Address>1401 S Main St</Address>
  11.  
  12. <City>Normal</City>
  13.  
  14. <State>IL</State>
  15.  
  16. <Phone>(309) 664-2846</Phone>
  17.  
  18. <Latitude>40.496254</Latitude>
  19.  
  20. <Longitude>-88.994449</Longitude>
  21.  
  22.  
  23. <Rating>
  24.  
  25. <AverageRating>5</AverageRating>
  26.  
  27. <TotalRatings>1</TotalRatings>
  28.  
  29. <TotalReviews>1</TotalReviews>
  30.  
  31. <LastReviewDate>1165801320</LastReviewDate>
  32.  
  33.  
  34. <LastReviewIntro>
  35.  
  36. all employees that i have come in contact with here have been kind and most importantly: very helpful!!
  37.  
  38. </LastReviewIntro>
  39.  
  40. </Rating>
  41.  
  42. <Distance>1.26</Distance>
  43.  
  44.  
  45. <Url>
  46.  
  47. http://local.yahoo.com/info-17548186-us-bank-normal
  48.  
  49. </Url>
  50.  
  51.  
  52. <ClickUrl>
  53.  
  54. http://local.yahoo.com/info-17548186-us-bank-normal
  55.  
  56. </ClickUrl>
  57.  
  58.  
  59. <MapUrl>
  60.  
  61. http://maps.yahoo.com/maps_result?q1=14 ... 1=17548186
  62.  
  63. </MapUrl>
  64.  
  65. <BusinessUrl>http://www.usbank.com/</BusinessUrl>
  66.  
  67. <BusinessClickUrl>http://www.usbank.com/</BusinessClickUrl>
  68.  
  69.  
  70. <Categories>
  71.  
  72. <Category id="96925947">Banks</Category>
  73.  
  74. </Categories>
  75.  
  76. </Result>
  77.  
  78.  
  79. <Result id="17544058">
  80.  
  81. <Title>Commerce Bank ATM</Title>
  82.  
  83. <Address>210 Broadway St</Address>
  84.  
  85. <City>Normal</City>
  86.  
  87. <State>IL</State>
  88.  
  89. <Phone>(309) 823-7364</Phone>
  90.  
  91. <Latitude>40.509374</Latitude>
  92.  
  93. <Longitude>-88.985691</Longitude>
  94.  
  95.  
  96. <Rating>
  97.  
  98. <AverageRating>NaN</AverageRating>
  99.  
  100. <TotalRatings>0</TotalRatings>
  101.  
  102. <TotalReviews>0</TotalReviews>
  103.  
  104. <LastReviewDate/>
  105.  
  106. <LastReviewIntro/>
  107.  
  108. </Rating>
  109.  
  110. <Distance>0.42</Distance>
  111.  
  112.  
  113. <Url>
  114.  
  115. http://local.yahoo.com/info-17544058-co ... atm-normal
  116.  
  117. </Url>
  118.  
  119.  
  120. <ClickUrl>
  121.  
  122. http://local.yahoo.com/info-17544058-co ... atm-normal
  123.  
  124. </ClickUrl>
  125.  
  126.  
  127. <MapUrl>
  128.  
  129. http://maps.yahoo.com/maps_result?q1=21 ... 1=17544058
  130.  
  131. </MapUrl>
  132.  
  133. <BusinessUrl>http://www.commercebank.com/</BusinessUrl>
  134.  
  135. <BusinessClickUrl>http://www.commercebank.com/</BusinessClickUrl>
  136.  
  137.  
  138. <Categories>
  139.  
  140. <Category id="96925947">Banks</Category>
  141.  
  142. <Category id="96934297">B2B Banks</Category>
  143.  
  144. </Categories>
  145.  
  146. </Result>
  147.  
  148. </ResultSet>
  149.  
  150.  
  151. <!--
  152.  
  153. ws03.ydn.ac4.yahoo.com compressed/chunked Tue Mar 23 19:57:56 PDT 2010
  154.  
  155. -->
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.:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.warriorpoint.androidxmlsimple;
  2.  
  3. import java.io.IOException;
  4.  
  5. import java.io.InputStream;
  6.  
  7. import java.net.MalformedURLException;
  8.  
  9. import java.net.URL;
  10.  
  11. import java.util.ArrayList;
  12.  
  13. import java.util.List;
  14.  
  15.  
  16.  
  17. import com.warriorpoint.androidxmlsimple.Message;
  18.  
  19.  
  20.  
  21. import android.sax.Element;
  22.  
  23. import android.sax.EndElementListener;
  24.  
  25. import android.sax.EndTextElementListener;
  26.  
  27. import android.sax.RootElement;
  28.  
  29. import android.util.Xml;
  30.  
  31.  
  32.  
  33. public class BaseFeedParser {
  34.  
  35.  
  36.  
  37.         static String feedUrlString = "http://local.yahooapis.com/LocalSearchService/V3/localSearch?appid=YahooDemo" +
  38.  
  39.         "&query=bank&latitude=40.5141667&longitude=-88.9905556&radius=2&results=5";
  40.  
  41.        
  42.  
  43.         // names of the XML tags
  44.  
  45.  
  46.  
  47.         static final String RESULTSET = "ResultSet";
  48.  
  49.         static final String RESULT = "Result"; 
  50.  
  51.        
  52.  
  53.         static final String TITLE = "Title";
  54.  
  55.         static final String ADDRESS = "Address";               
  56.  
  57.         private final URL feedUrl;
  58.  
  59.  
  60.  
  61.         protected BaseFeedParser(){
  62.  
  63.                 try {
  64.  
  65.                         this.feedUrl = new URL(feedUrlString);
  66.  
  67.                 } catch (MalformedURLException e) {
  68.  
  69.                         throw new RuntimeException(e);
  70.  
  71.                 }
  72.  
  73.         }
  74.  
  75.  
  76.  
  77.         protected InputStream getInputStream() {
  78.  
  79.                 try {
  80.  
  81.                         return feedUrl.openConnection().getInputStream();
  82.  
  83.                 } catch (IOException e) {
  84.  
  85.                         throw new RuntimeException(e);
  86.  
  87.                 }
  88.  
  89.         }
  90.  
  91.        
  92.  
  93.         public List<Message> parse() {
  94.  
  95.                 final Message currentMessage = new Message();
  96.  
  97.                 RootElement root = new RootElement(RESULTSET);
  98.  
  99.                 final List<Message> messages = new ArrayList<Message>();
  100.  
  101.                 Element itemlist = root.getChild(RESULT);
  102.  
  103.  
  104.  
  105.                 itemlist.setEndElementListener(new EndElementListener(){
  106.  
  107.                         public void end() {
  108.  
  109.                                 messages.add(currentMessage.copy());
  110.  
  111.                         }
  112.  
  113.                 });
  114.  
  115.                 itemlist.getChild(TITLE).setEndTextElementListener(new EndTextElementListener(){
  116.  
  117.                         public void end(String body) {
  118.  
  119.                                 currentMessage.setTitle(body);
  120.  
  121.                         }
  122.  
  123.                 });
  124.  
  125.                
  126.  
  127.                
  128.  
  129.                
  130.  
  131.                 try {
  132.  
  133.                         Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
  134.  
  135.                 } catch (Exception e) {
  136.  
  137.                         throw new RuntimeException(e);
  138.  
  139.                 }
  140.  
  141.                 return messages;
  142.  
  143.         }
  144.  
  145. }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.warriorpoint.androidxmlsimple;
  2.  
  3.  
  4.  
  5. import java.util.Date;
  6.  
  7.  
  8.  
  9. public class Message implements Comparable<Message>{
  10.  
  11.  
  12.  
  13.         private String title;
  14.  
  15.         private String address;
  16.  
  17.         private String distance;
  18.  
  19.         private String phone;
  20.  
  21.  
  22.  
  23.         public String getTitle() {
  24.  
  25.                 return title;
  26.  
  27.         }
  28.  
  29.  
  30.  
  31.         public void setTitle(String Title) {
  32.  
  33.                 this.title = title.trim();
  34.  
  35.         }
  36.  
  37.         // getters and setters omitted for brevity
  38.  
  39. //      public URL getLink() {
  40.  
  41. //              return link;
  42.  
  43. //      }
  44.  
  45. //     
  46.  
  47. //      public void setLink(String link) {
  48.  
  49. //              try {
  50.  
  51. //                      this.link = new URL(link);
  52.  
  53. //              } catch (MalformedURLException e) {
  54.  
  55. //                      throw new RuntimeException(e);
  56.  
  57. //              }
  58.  
  59. //      }
  60.  
  61.         public String getAddress() {
  62.  
  63.                 return address;
  64.  
  65.         }
  66.  
  67.  
  68.  
  69.         public void setAddress(String Address) {
  70.  
  71.                 this.address = Address.trim();
  72.  
  73.         }
  74.  
  75.        
  76.  
  77.  
  78.  
  79.         public String getPhone() {
  80.  
  81.                 return phone;
  82.  
  83.         }
  84.  
  85.  
  86.  
  87.         public void setPhone(String Phone) {
  88.  
  89.                 this.phone = Phone.trim();
  90.  
  91.         }
  92.  
  93.        
  94.  
  95.         public String getDistance() {
  96.  
  97.                 return distance;
  98.  
  99.         }
  100.  
  101.  
  102.  
  103.         public void setDistance(String Distance) {
  104.  
  105.                 this.distance = Distance.trim();
  106.  
  107.         }
  108.  
  109.        
  110.  
  111.         public Message copy(){
  112.  
  113.                 Message copy = new Message();
  114.  
  115.                 copy.title = title;
  116.  
  117.                 return copy;
  118.  
  119.         }
  120.  
  121.        
  122.  
  123.         @Override
  124.  
  125.         public String toString() {
  126.  
  127.                 StringBuilder sb = new StringBuilder();
  128.  
  129.                 sb.append("Title: ");
  130.  
  131.                 sb.append(title);
  132.  
  133.                 return sb.toString();
  134.  
  135.         }
  136.  
  137.  
  138.  
  139. //      @Override
  140.  
  141. //      public int hashCode() {
  142.  
  143. //              final int prime = 31;
  144.  
  145. //              int result = 1;
  146.  
  147. //              result = prime * result + ((date == null) ? 0 : date.hashCode());
  148.  
  149. //              result = prime * result
  150.  
  151. //                              + ((description == null) ? 0 : description.hashCode());
  152.  
  153. //              result = prime * result + ((link == null) ? 0 : link.hashCode());
  154.  
  155. //              result = prime * result + ((title == null) ? 0 : title.hashCode());
  156.  
  157. //              return result;
  158.  
  159. //      }
  160.  
  161.        
  162.  
  163.         @Override
  164.  
  165.         public boolean equals(Object obj) {
  166.  
  167.                 if (this == obj)
  168.  
  169.                         return true;
  170.  
  171.                 if (obj == null)
  172.  
  173.                         return false;
  174.  
  175.                 if (getClass() != obj.getClass())
  176.  
  177.                         return false;
  178.  
  179.                 Message other = (Message) obj;
  180.  
  181.                
  182.  
  183.                 if (title == null) {
  184.  
  185.                         if (other.title != null)
  186.  
  187.                                 return false;
  188.  
  189.                 } else if (!title.equals(other.title))
  190.  
  191.                         return false;
  192.  
  193.                 return true;
  194.  
  195.         }
  196.  
  197.  
  198.  
  199. @Override
  200.  
  201. public int compareTo(Message arg0) {
  202.  
  203.         // TODO Auto-generated method stub
  204.  
  205.         return 0;
  206.  
  207. }
  208.  
  209.  
  210.  
  211.        
  212.  
  213. }
Parsed in 0.042 seconds, using GeSHi 1.0.8.4



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.warriorpoint.androidxmlsimple;
  2.  
  3.  
  4.  
  5. import java.util.ArrayList;
  6.  
  7. import java.util.List;
  8.  
  9. import android.app.ListActivity;
  10.  
  11. import android.os.Bundle;
  12.  
  13. import android.util.Log;
  14.  
  15.  
  16.  
  17. import android.widget.ArrayAdapter;
  18.  
  19.  
  20.  
  21.  
  22.  
  23. public class MessageList extends ListActivity {
  24.  
  25.        
  26.  
  27.         private List<Message> messages;
  28.  
  29.        
  30.  
  31.     @Override
  32.  
  33.     public void onCreate(Bundle icicle) {
  34.  
  35.         super.onCreate(icicle);
  36.  
  37.         setContentView(R.layout.main);
  38.  
  39.         loadFeed();
  40.  
  41.     }
  42.  
  43.  
  44.  
  45.         private void loadFeed(){
  46.  
  47.         try{
  48.  
  49.                 BaseFeedParser parser = new BaseFeedParser();
  50.  
  51.                 messages = parser.parse();
  52.  
  53.                 List<String> titles = new ArrayList<String>(messages.size());
  54.  
  55.                 for (Message msg : messages){
  56.  
  57.                         titles.add(msg.getTitle());
  58.  
  59.                 }
  60.  
  61.                 ArrayAdapter<String> adapter =
  62.  
  63.                         new ArrayAdapter<String>(this, R.layout.row,titles);
  64.  
  65.                 this.setListAdapter(adapter);
  66.  
  67.         } catch (Throwable t){
  68.  
  69.                 Log.e("Business Locator",t.getMessage(),t);
  70.  
  71.         }
  72.  
  73.     }
  74.  
  75.    
  76.  
  77. }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.warriorpoint.androidxmlsimple;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import java.util.List;
  6.  
  7.  
  8.  
  9. import org.xml.sax.Attributes;
  10.  
  11. import org.xml.sax.SAXException;
  12.  
  13. import org.xml.sax.helpers.DefaultHandler;
  14.  
  15.  
  16.  
  17. import static com.warriorpoint.androidxmlsimple.BaseFeedParser.*;
  18.  
  19.  
  20.  
  21. public class RssHandler extends DefaultHandler{
  22.  
  23.         private List<Message> messages;
  24.  
  25.         private Message currentMessage;
  26.  
  27.         private StringBuilder builder;
  28.  
  29.        
  30.  
  31.         public List<Message> getMessages(){
  32.  
  33.                 return this.messages;
  34.  
  35.         }
  36.  
  37.         @Override
  38.  
  39.         public void characters(char[] ch, int start, int length)
  40.  
  41.                         throws SAXException {
  42.  
  43.                 super.characters(ch, start, length);
  44.  
  45.                 builder.append(ch, start, length);
  46.  
  47.         }
  48.  
  49.  
  50.  
  51.         @Override
  52.  
  53.         public void endElement(String uri, String localName, String name)
  54.  
  55.                         throws SAXException {
  56.  
  57.                 super.endElement(uri, localName, name);
  58.  
  59.                 if (this.currentMessage != null){
  60.  
  61.                         if (localName.equalsIgnoreCase("Title")){
  62.  
  63.                                 currentMessage.setTitle(builder.toString());
  64.  
  65. //                      } else if (localName.equalsIgnoreCase(ADDRESS)){
  66.  
  67. //                              currentMessage.setAddress(builder.toString());
  68.  
  69. //                      } else if (localName.equalsIgnoreCase(PHONE)){
  70.  
  71. //                              currentMessage.setPhone(builder.toString());
  72.  
  73. //                      } else if (localName.equalsIgnoreCase(DISTANCE)){
  74.  
  75. //                              currentMessage.setDistance(builder.toString());
  76.  
  77. //                      } else if (localName.equalsIgnoreCase(ITEM)){
  78.  
  79. //                              messages.add(currentMessage);
  80.  
  81.                         }
  82.  
  83.                         builder.setLength(0);  
  84.  
  85.                 }
  86.  
  87.         }
  88.  
  89.  
  90.  
  91.         @Override
  92.  
  93.         public void startDocument() throws SAXException {
  94.  
  95.                 super.startDocument();
  96.  
  97.                 messages = new ArrayList<Message>();
  98.  
  99.                 builder = new StringBuilder();
  100.  
  101.         }
  102.  
  103.  
  104.  
  105.         @Override
  106.  
  107.         public void startElement(String uri, String localName, String name,
  108.  
  109.                         Attributes attributes) throws SAXException {
  110.  
  111.                 super.startElement(uri, localName, name, attributes);
  112.  
  113.                 if (localName.equalsIgnoreCase("Title")){
  114.  
  115.                         this.currentMessage = new Message();
  116.  
  117.                 }
  118.  
  119.         }
  120.  
  121. }
Parsed in 0.042 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
mmgill
Junior Developer
Junior Developer
 
Posts: 20
Joined: Thu Mar 04, 2010 8:40 pm

Top

Re:

Postby songotho » Thu Mar 25, 2010 8:18 am

Hey men,
U can follow some example in this topic of 4rum:



It maybe help u.

Best Regard,
Alex
songotho
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Mar 03, 2009 1:59 am

Postby pria » Mon Mar 29, 2010 1:22 pm

Try making these changes in the following function of RssHandler class:

public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.equals("Result")) {
currentMessage= new Message();
messages.add(0,currentMessage);
}
}


public void characters(char[] ch, int start, int length) throws SAXException {
String chars = new String(ch, start, length).trim();

if (chars.length() > 0) {
if (localName.equals("Title")) {
currentMessage.setTitle(chars.toString());
}else if (localName.equals("Distance")) {
currentMessage.setDistance(chars.toString());
}
}
}

Hope it helps.
pria
Junior Developer
Junior Developer
 
Posts: 14
Joined: Tue Mar 02, 2010 1:37 pm
Location: Pune

Postby songotho » Mon Mar 29, 2010 4:22 pm

pria wrote:Try making these changes in the following function of RssHandler class:

public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.equals("Result")) {
currentMessage= new Message();
messages.add(0,currentMessage);
}
}


public void characters(char[] ch, int start, int length) throws SAXException {
String chars = new String(ch, start, length).trim();

if (chars.length() > 0) {
if (localName.equals("Title")) {
currentMessage.setTitle(chars.toString());
}else if (localName.equals("Distance")) {
currentMessage.setDistance(chars.toString());
}
}
}

Hope it helps.


Hi,
I dont understand whatever u want, some functions use to read the xml file, would u pls tell detail about it.
Alex.
songotho
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Mar 03, 2009 1:59 am

Postby pria » Wed Mar 31, 2010 6:30 am

You have posted the code for RssHandler. Try replacing ur startElement() and characters() wid my sample code.
pria
Junior Developer
Junior Developer
 
Posts: 14
Joined: Tue Mar 02, 2010 1:37 pm
Location: Pune

hey

Postby mmgill » Wed Mar 31, 2010 3:40 pm

i did and it doesnt seem to help any. unless im not adding it correctly..
mmgill
Junior Developer
Junior Developer
 
Posts: 20
Joined: Thu Mar 04, 2010 8:40 pm

Top

Re: hey

Postby songotho » Wed Mar 31, 2010 7:30 pm

mmgill wrote:i did and it doesnt seem to help any. unless im not adding it correctly..


^^ U can try with some codes follow:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.alex.xml;
  3.  
  4.  
  5.  
  6. import java.io.IOException;
  7.  
  8. import java.io.InputStream;
  9.  
  10. import java.net.MalformedURLException;
  11.  
  12. import java.net.URL;
  13.  
  14. import java.util.Vector;
  15.  
  16.  
  17.  
  18. import javax.xml.parsers.SAXParser;
  19.  
  20. import javax.xml.parsers.SAXParserFactory;
  21.  
  22.  
  23.  
  24. import org.xml.sax.Attributes;
  25.  
  26. import org.xml.sax.InputSource;
  27.  
  28. import org.xml.sax.SAXException;
  29.  
  30. import org.xml.sax.XMLReader;
  31.  
  32. import org.xml.sax.helpers.DefaultHandler;
  33.  
  34.  
  35.  
  36. import android.app.Activity;
  37.  
  38. import android.os.Bundle;
  39.  
  40. import android.widget.TextView;
  41.  
  42.  
  43.  
  44. public class XML extends Activity {
  45.  
  46.     /** Called when the activity is first created. */
  47.  
  48.     @Override
  49.  
  50.     public void onCreate(Bundle savedInstanceState) {
  51.  
  52.         super.onCreate(savedInstanceState);
  53.  
  54.         //setContentView(R.layout.main);
  55.  
  56.        
  57.  
  58.         TextView tv = new TextView(this);
  59.  
  60.         try {
  61.  
  62.                         URL url = new URL ("http://local.yahooapis.com/LocalSearchService/V3/localSearch?appid=YahooDemo&query=bank&latitude=40.5141667&longitude=-88.9905556&radius=2&results=4");
  63.  
  64.                         Vector<ParsedExampleDataSet> test = read(url.openStream());
  65.  
  66.                         String text = "";
  67.  
  68.                         for (int i=0; i<test.size(); i++) {
  69.  
  70.                                 text += test.get(i).getTitle()+"\n";
  71.  
  72.                         }
  73.  
  74.                         tv.setText(text);
  75.  
  76.                 } catch (MalformedURLException e) {
  77.  
  78.                 } catch (IOException e) {
  79.  
  80.                 }
  81.  
  82.         this.setContentView(tv);
  83.  
  84.     }
  85.  
  86.    
  87.  
  88.     public Vector<ParsedExampleDataSet> read(InputStream in) {
  89.  
  90.         SAXParserFactory spf = SAXParserFactory.newInstance();
  91.  
  92.         SAXParser sp;
  93.  
  94.         try {
  95.  
  96.                         sp = spf.newSAXParser();
  97.  
  98.                         XMLReader xr = sp.getXMLReader();
  99.  
  100.                         ContentHandler ch = new ContentHandler();
  101.  
  102.                         xr.setContentHandler(ch);
  103.  
  104.                         xr.parse(new InputSource(in));
  105.  
  106.                         return ch.getPDataSet();
  107.  
  108.                 } catch (Exception e) {
  109.  
  110.                 }
  111.  
  112.                 return null;
  113.  
  114.     }
  115.  
  116. }
  117.  
  118.  
  119.  
  120. class ParsedExampleDataSet {
  121.  
  122.         private String Title;
  123.  
  124.        
  125.  
  126.         public String getTitle() { return Title; }
  127.  
  128.         public void setTitle(String Title) { this.Title = Title; }
  129.  
  130. }
  131.  
  132.  
  133.  
  134. class ContentHandler extends DefaultHandler {
  135.  
  136.         private boolean in_titleTag = false;
  137.  
  138.        
  139.  
  140.         private ParsedExampleDataSet DataSet;
  141.  
  142.         private Vector<ParsedExampleDataSet> myPEDataSet;
  143.  
  144.        
  145.  
  146.         public ContentHandler() {
  147.  
  148.                 super();
  149.  
  150.                 this.myPEDataSet = new Vector<ParsedExampleDataSet>();
  151.  
  152.         }
  153.  
  154.         public Vector<ParsedExampleDataSet> getPDataSet() {
  155.  
  156.                 return this.myPEDataSet;
  157.  
  158.         }
  159.  
  160.        
  161.  
  162.         public void startDocument() throws SAXException { }
  163.  
  164.     public void endDocument() throws SAXException { }
  165.  
  166.    
  167.  
  168.     @Override
  169.  
  170.     public void startElement(String n, String l, String q, Attributes atts) throws SAXException {
  171.  
  172.         if (l.equals("Title")) {
  173.  
  174.                 this.in_titleTag = true;
  175.  
  176.         }
  177.  
  178.         if (l.equals("Result")) {
  179.  
  180.                 DataSet = new ParsedExampleDataSet();
  181.  
  182.         }
  183.  
  184.     }
  185.  
  186.    
  187.  
  188.     @Override
  189.  
  190.     public void endElement(String n, String l, String q) throws SAXException {
  191.  
  192.         if (l.equals("Title")) {
  193.  
  194.                 this.in_titleTag = false;
  195.  
  196.         }
  197.  
  198.         if (l.equals("Result")) {
  199.  
  200.                 myPEDataSet.add(DataSet);
  201.  
  202.         }
  203.  
  204.     }
  205.  
  206.    
  207.  
  208.     @Override
  209.  
  210.     public void characters(char[] ch, int start, int length) throws SAXException {
  211.  
  212.         if (in_titleTag) {
  213.  
  214.                 DataSet.setTitle(new String(ch, start, length));
  215.  
  216.         }
  217.  
  218.     }
  219.  
  220. }
  221.  
  222.  
Parsed in 0.051 seconds, using GeSHi 1.0.8.4


Use:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. <uses-permission android:name="android.permission.INTERNET"/>
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
in AndroidManifest.xml.

Hope help u.
Alex.
songotho
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Mar 03, 2009 1:59 am

Postby pria » Thu Apr 01, 2010 6:45 am

Hey Alex,

Tried ur code...Yeah it code works :)
Last edited by pria on Mon Apr 05, 2010 9:55 am, edited 2 times in total.
pria
Junior Developer
Junior Developer
 
Posts: 14
Joined: Tue Mar 02, 2010 1:37 pm
Location: Pune

xml help

Postby mmgill » Sun Apr 04, 2010 6:39 pm

Hi guys,

Thanks for all the help. It works great. I see how you just add the next result to a textview with a break between them. Is there a way to make the textview items clickable? Or is there a way to add the items to a listview by modifying the code?
Or i guess another option would be to store each result as an object so that they could be put in a listview? Which one would work best>? Any help would be great.

Thanks again for the help.

MG
mmgill
Junior Developer
Junior Developer
 
Posts: 20
Joined: Thu Mar 04, 2010 8:40 pm

Postby pria » Mon Apr 05, 2010 9:56 am

We can make a text view clickable...by adding a OnClickListener on the textView.
pria
Junior Developer
Junior Developer
 
Posts: 14
Joined: Tue Mar 02, 2010 1:37 pm
Location: Pune

xml

Postby mmgill » Mon Apr 05, 2010 9:08 pm

alright. so would i have to make a clicklistener for each item when they are added to a list. because when i click on one i want its location to be shown on a mapview. would i have to store the latitude and longitude when i parsed the xml? would it have been easier to parse the data and save it as an object(with title, lat, long, address, etc) and reference it from there to plot on the mapview?>

Thanks for all the help guys!
mmgill
Junior Developer
Junior Developer
 
Posts: 20
Joined: Thu Mar 04, 2010 8:40 pm

found something

Postby mmgill » Mon Apr 05, 2010 10:28 pm

so if i wanted to use an example like the one below would i be able to modify it so that it stores info about an object (title, address, latitude, longitude) that would then be able to add to an arraylist and then added to a listview. In this example they parse an rss feed and display the title in a listview. could someone maybe look over this and see if it would be easier to change this (or if its possible for my xml file) or just use the code that was provided above?

thanks guys. im slowly getting better at this. bare with me

Mg
Attachments
AndroidXmlSimple.zip
parses rss xml. can it be modified to fit my xml?
(47.36 KiB) Downloaded 76 times
mmgill
Junior Developer
Junior Developer
 
Posts: 20
Joined: Thu Mar 04, 2010 8:40 pm

Parsing xml

Postby Richard.Williams » Wed Apr 07, 2010 4:25 pm

This would have been smaller code with biterscripting.



Code: Select all
var str xml, title, distance
cat "/path/to/xml file.xml" >  $xml
while ( { sen "^<Title>^" $xml } > 0 )
do
    stex -r -c "^<Title&\>&</Title&\>^" $xml > $title
    stex "^>^]" $title > null ; stex "[^<^" $title > null
    stex -r -c "^<Distance&\>&</Distance&\>^" $xml > $distance
    stex "^>^]" $distance > null ; stex "[^<^" $distance > null
    echo $title "\t" $distance
done





We use java and biterscripting for parsing xml files. Not sure if it will work on Android platform.
Richard.Williams
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Apr 07, 2010 4:13 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests