selecting specific xml tags with SAX parser

瘦欲@ 提交于 2019-12-13 21:14:19

问题


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

回答1:


If I were you then I would just use the Simple 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: you can read it here.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/5796041/selecting-specific-xml-tags-with-sax-parser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!