选择具有SAX解析器特定的XML标签 [英] selecting specific xml tags with SAX parser

查看:125
本文介绍了选择具有SAX解析器特定的XML标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计的Andr​​oid应用程序,我需要解析一个XML文件,我已经告诉SAX解析器是为移动设备更好地我一直在试图使用它,但我已经得到了坚持。下面是XML code:

 < current_conditions>
    <条件数据=清除/>
    < temp_f数据=50/>
    < temp_c数据=10/>
    <湿度数据=湿度:76%/>
    <图标数据=/ IG /图像/天气/ sunny.gif/>
    < wind_condition数据=风:东北,14英里每小时/>
  < / current_conditions>  < forecast_conditions>
    < D​​AY_OF_WEEK数据=星期二/> ****
    <低数据=43/>
    <高数据=64/> ****
    <图标数据=/ IG /图像/天气/ mostly_sunny.gif/>
    <条件数据=晴/>
  < / forecast_conditions>  < forecast_conditions>
    < D​​AY_OF_WEEK数据=星期三/>
    <低数据=43/>
    <高数据=64/>
    <图标数据=/ IG /图像/天气/ sunny.gif/>
    <条件数据=清除/>
  < / forecast_conditions>

我想通过侧面得到的只有两个变量的数值是 * ,但它在文档,而不是结束返回值。如何,我只希望在XML某些价值我解决这个问题。这里是我的code:

 公共类ExampleHandler扩展的DefaultHandler {私人布尔in_in_current_conditions = FALSE;
私人布尔in_in_forecast_conditions = FALSE;私人ParsedExampleDataSet myParsedExampleDataSet =新ParsedExampleDataSet();
公共ParsedExampleDataSet getParsedData(){
    返回this.myParsedExampleDataSet;
}
@覆盖
公共无效startDocument()抛出的SAXException {
    this.myParsedExampleDataSet =新ParsedExampleDataSet();
}
@覆盖
公共无效调用endDocument()抛出的SAXException {
}
@覆盖
公共无效的startElement(字符串的namespaceURI,字符串的localName,
        串QNAME,属性的ATT)抛出的SAXException
{
    如果(localName.equals(forecast_information))
    {
        this.in_forecast_information = TRUE;
    }
    否则如果(localName.equals(current_conditions))
    {
        this.in_in_current_conditions = TRUE;
    }
    否则如果(localName.equals(forecast_conditions))
    {
        this.in_in_forecast_conditions = TRUE;
    }
    否则如果(localName.equals(高)){
        如果(this.in_in_forecast_conditions)
        {
            字符串attrValue = atts.getValue(数据);
            myParsedExampleDataSet.setCurrtempValue(attrValue);
        }
    }否则如果(localName.equals(DAY_OF_WEEK)){
        如果(this.in_in_forecast_conditions){
            字符串attrValue1 = atts.getValue(数据);
            myParsedExampleDataSet.setLowValue(attrValue1);
        }
    }
}@覆盖
公共无效的endElement(字符串的namespaceURI,字符串的localName,字符串QNAME)
        抛出的SAXException {
    如果(localName.equals(forecast_information)){
        this.in_forecast_information = FALSE;
    }否则如果(localName.equals(current_conditions)){
        this.in_in_current_conditions = FALSE;
    }否则如果(localName.equals(forecast_conditions)){
        this.in_in_forecast_conditions = FALSE;
    }
}@覆盖
公共无效字符(字符CH [],诠释开始,诠释长度){
}
 }


解决方案

我已经通过使用数组填充值,当我想显示他们,我简单的使用他们的阵列中的位置,打电话给他们排序这个

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

解决方案

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

这篇关于选择具有SAX解析器特定的XML标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆