开发者

Cannot get XML handler to read Temperaturetags when parseing from web

开发者 https://www.devze.com 2023-03-23 14:22 出处:网络
trying to create an android app but the SAXparser won\'t recognise and display the tempertaure tag data fourther down the document with setting up error and ive tryied everything i can think of to fix

trying to create an android app but the SAXparser won't recognise and display the tempertaure tag data fourther down the document with setting up error and ive tryied everything i can think of to fix it

Code xml file

<weatherdata>
    <timetags>
     <item name="date">
        <value>26/07/2011</value>
        <unit/>
        <image/>
        <class>dynamic</class>
        <description>The current date</description>
        </item>
    </timetags>
    <temperaturetags>
      <item name="temp">
         <value>21.8</value>
    </temperaturetags>
 </weatherdata>

XML handler

    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;

    public class XMLhandler extends DefaultHandler {

Boolean currentElement = false;
Boolean temperature = false;
String currentValue = null;
public static weatherlist sitesList = null;

public static weatherlist getSitesList() {
return sitesList;
}

public static void setSitesList(weatherlist sitesList) {
XMLhandler.sitesList = sitesList;
}

@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {


if (localName.equals("weatherdata"))
{
    currentElement = true;
    /** Start*/
    sitesList = new weatherlist();
} if (localName.equals("temperaturetags")) {
    temperature = true;
    /** Start
    sitesList = new weatherlist();*/
} else if (localName.equals("item")) {
/** Get attribute value */
String attr = attributes.getValue("name");
sitesList.setValue(attr);
}

}

/** Called when tag closing*/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {

    currentElement = false;

if (localName.equals("weatherdata"))
    {
    //  currentElement = false;
    } else if (localName.equalsIgnoreCase("temperaturetags")) {
        this.temperature = false;
        sitesList.settemperature(currentValue);
    } else if (localName.equalsIgnoreCase("item")) {
        sitesList.setName(currentValue);
    } else if (localName.equalsIgnoreCase("value")){
            sitesList.setitem(currentValue);}
    }

    //  currentElement = false;

/** set value 
if (localName.equalsIgnoreCase("item"))
sitesList.setName(currentValue);
else if (localName.equalsIgnoreCase("value"))
sitesList.setitem(currentValue);

}*/

/** Called to get tag characters */
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {

if (currentElement) {
currentValue = new String(ch, start, length);
currentElement = false;
}        

code: arraylist

    import java.util.ArrayList;


    public class weatherlist {

private ArrayList<String> temperature = new ArrayList<String>();
private ArrayList<String> value = new ArrayList<String>();
private ArrayList<String> name = new ArrayList<String>();
private ArrayList<String> item = new ArrayList<String>();

/** In Setter method default it will return arraylist
* change that to add */

public ArrayList<String> gettemperature() {
    return 开发者_Python百科temperature;
    }

    public void settemperature(String temperature) {
    this.temperature.add(temperature);
    }

public ArrayList<String> getvalue() {
return value;
}

public void setValue(String value) {
this.value.add(value);
}

public ArrayList<String> getName() {
    return name;
    }

    public void setName(String name) {
    this.name.add(name);
    }

public ArrayList<String> getitem() {
return item;
}

public void setitem(String item) {
this.item.add(item);
}

    }

Someone please help! Thank you in advance


Try closing your item tag

<temperaturetags>
      <item name="temp">
         <value>21.8</value>
      </item>
</temperaturetags>
0

精彩评论

暂无评论...
验证码 换一张
取 消