19166

selecting specific xml tags with SAX parser

Question:

I'm designing an App for android and I need to parse an XML file, I've been told SAX parsers are better for mobile devices to I've been trying to use it but I've gotten stuck. Here is the XML code:

<current_conditions> <condition data="Clear"/> <temp_f data="50"/> <temp_c data="10"/> <humidity data="Humidity: 76%"/> <icon data="/ig/images/weather/sunny.gif"/> <wind_condition data="Wind: NE at 14 mph"/> </current_conditions> <forecast_conditions> <day_of_week data="Tue"/> **** <low data="43"/> <high data="64"/> **** <icon data="/ig/images/weather/mostly_sunny.gif"/> <condition data="Mostly Sunny"/> </forecast_conditions> <forecast_conditions> <day_of_week data="Wed"/> <low data="43"/> <high data="64"/> <icon data="/ig/images/weather/sunny.gif"/> <condition data="Clear"/> </forecast_conditions>

I am trying to get values of only the two tags with <strong>*</strong> by the side but it returns the values at the end of the document instead. How do I solve this problem as I only want certain values in the XML. Here is my code:

public class ExampleHandler extends DefaultHandler { private boolean in_in_current_conditions = false; private boolean in_in_forecast_conditions = false; private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet(); public ParsedExampleDataSet getParsedData() { return this.myParsedExampleDataSet; } @Override public void startDocument() throws SAXException { this.myParsedExampleDataSet = new ParsedExampleDataSet(); } @Override public void endDocument() throws SAXException { } @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if (localName.equals("forecast_information")) { this.in_forecast_information = true; } else if (localName.equals("current_conditions")) { this.in_in_current_conditions = true; } else if (localName.equals("forecast_conditions")) { this.in_in_forecast_conditions = true; } else if (localName.equals("high")) { if (this.in_in_forecast_conditions) { String attrValue = atts.getValue("data"); myParsedExampleDataSet.setCurrtempValue(attrValue); } } else if (localName.equals("day_of_week")) { if (this.in_in_forecast_conditions) { String attrValue1 = atts.getValue("data"); myParsedExampleDataSet.setLowValue(attrValue1); } } } @Override public void endElement(String namespaceURI, String localName, String qName) throws SAXException { if (localName.equals("forecast_information")) { this.in_forecast_information = false; } else if (localName.equals("current_conditions")) { this.in_in_current_conditions = false; } else if (localName.equals("forecast_conditions")) { this.in_in_forecast_conditions = false; } } @Override public void characters(char ch[], int start, int length) { } }

Answer1:

If I were you then I would just use the <a href="http://simple.sourceforge.net/" rel="nofollow">Simple</a> XML framework to do the XML parsing work for you. It would not be too difficult then to just create a few objects that could tease this data out of the XML.

P.S. I use the Simple project for all my XML so I even wrote a blog post explaining how to use it in Android projects: <a href="http://massaioli.homelinux.com/wordpress/2011/04/21/simple-xml-in-android-1-5-and-up/" rel="nofollow">you can read it here</a>.

Answer2:

To get those two values you would do something like this:

public class ExampleHandler extends DefaultHandler { private static final String FORECAST_CONDITION = "forecast_condition"; private boolean day_of_week_dataWanted = false; private boolean high_dataWanted = false; private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet(); public ParsedExampleDataSet getParsedData() { return this.myParsedExampleDataSet; } public ExampleHandler() { this.myParsedExampleDataSet = new ParsedExampleDataSet(); } @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if(FORECAST_CONDITION.equals(localName)){ if("day_of_week".equals(localName)){ day_of_week_dataWanted = true; } if("high".equals(localName)){ high_dataWanted = true; } } } @Override public void characters(char ch[], int start, int length) { if(day_of_week_dataWanted){ myParsedExampleDataSet.setLowValue(new String(ch, start, length)); day_of_week_dataWanted = false; } if(high_dataWanted){ myParsedExampleDataSet.setCurrtempValue(new String(ch, start, length)); high_dataWanted = false; } } @Override public void endElement(String namespaceURI, String localName, String qName) throws SAXException { } }

But there are more graceful solutions you could look at, hopefully this gives you a hint.

Answer3:

I've sorted this by using populating the values in Arrays and when I want to display them I simple use their position in the array to call them

Recommend

  • RaptureXML cannot get to children tags
  • Error getting Images into Listview from server using Json
  • apache won't start
  • Converting a long date with time to mm-dd-yyyy hh:mm AM/PM
  • Consistent Client Side Date/timestamp using JavaScript(considering TimeZones)
  • Where can in find the locale objects for d3.js for different countries
  • MySQL multiple IN conditions to subquery with same table
  • Create function that can pass a parameter without making a new component
  • Is it possible to get the word under the mouse cursor in a ``?
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • How can we prepend rows to a react native list-view?
  • BeautifulSoup difference between findAll and findChildren
  • NHibernate manually control fetching
  • UWP/C# - Issue with AQS and USB Devices
  • How can I display the parent menu item's description using Wordpress walkers?
  • Ember.js model to be organised as a tree structure
  • Eloquent paginate function in Slim 3 project using twig
  • How to determine if there are bytes available to be read from boost:asio:serial_port
  • Retrieve list of sent friend requests from friend_request FQL table
  • How do I get HTML corresponding to current DOM tree?
  • java inputstream
  • Jackson Parser: ignore deserializing for type mismatch
  • JQuery Internet Explorer and ajaxstop
  • JSON response opens as a file, but I can't access it with JavaScript
  • OpenGL ES texture problem, 4 duplicate columns and horizontal lines (Android)
  • Can I check if a recipient has an automatic reply before I send an email?
  • Handling un-mapped Rest path
  • Using jQuery closest() method with class selector
  • Regex thinks I'm nesting, but I'm not
  • Fill an image in a square container while keeping aspect ratio
  • Change an a tag attribute in JavaScript based on screen width
  • Convert array of 8 bytes to signed long in C++
  • ORA-29908: missing primary invocation for ancillary operator
  • How can I estimate amount of memory left with calling System.gc()?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Understanding cpu registers
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices
  • Reading document lines to the user (python)