Search code examples
androidxmlparsingsaxsaxparser

selecting specific xml tags with SAX parser


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 * 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) {
}
 }

Solution

  • 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