0
我是在android中使用SAX解析器的新工作。問題是,我解析我的本地XML文件後,我想檢查它是否保留值與System.out.println
,但它返回null。android中的SAX解析器不會返回值
所以,基本上如果我嘗試ArrayList<PlaceEntry> pl = PlacesXMLHandler.places;
,然後檢查system.out.print(pl)
,它有一個空的數組。我試圖用PlaceEntry
做同樣的事情,它也給我null。我猜測我創建XMLHandler時犯了一個錯誤,但我不確定它在哪裏。
我的XML:
<?xml version="1.0" encoding="utf-8"?>
<nodes>
<entry>
<title>My Title</title>
<description>Description</description>
<webpage>www.google.com</webpage>
<coordinates>
<latitude>100.00</latitude>
<longitude>100.00</longitude>
</coordinates>
</entry>
</nodes>
我的擴展處理類:
public class PlacesXMLHandler extends DefaultHandler
{
private Boolean currentElement = false;
private String currentValue = null;
public static PlaceEntry placeEntry = null;
public static ArrayList<PlaceEntry> places = null;
private String[] coord = null;
public static PlaceEntry getPlaceEntry()
{
return placeEntry;
}
public static void setSitesList(PlaceEntry placeEntry)
{
PlacesXMLHandler.placeEntry = placeEntry;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException
{
currentElement = true;
if (localName.equals("nodes"))
{
places = new ArrayList<PlaceEntry>();
}
else if (localName.equals("entry"))
{
placeEntry = new PlaceEntry();
places.add(placeEntry);
}
else if (localName.equals("coordinates"))
{
coord = new String[2];
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException
{
currentElement = false;
if (localName.equalsIgnoreCase("title"))
placeEntry.setTitle(currentValue);
else if (localName.equalsIgnoreCase("description"))
placeEntry.setSubtitle(currentValue);
else if (localName.equalsIgnoreCase("webpage"))
placeEntry.setWebpage(currentValue);
else if (localName.equalsIgnoreCase("latitude"))
coord[0] = currentValue;
else if (localName.equalsIgnoreCase("longitude"))
coord[1] = currentValue;
if (!coord.equals(null))
placeEntry.setCoordinates(coord[0], coord[1]);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException
{
if (currentElement)
{
currentValue = new String(ch, start, length);
currentElement = false;
}
}
}
我PlaceEntry類:
public class PlaceEntry implements Serializable
{
public String title;
public String subtitle;
public String[] coord;
public String webpage;
public PlaceEntry()
{
setTitle(null);
setSubtitle(null);
setWebpage(null);
setCoordinates(null, null);
}
public void setTitle (String title)
{
this.title = title;
}
public String getTitle()
{
return title;
}
public void setSubtitle (String subtitle)
{
this.subtitle = subtitle;
}
public String getSubtitle()
{
return subtitle;
}
public void setWebpage (String webpage)
{
this.webpage = webpage;
}
public String getWebpage()
{
return webpage;
}
public void setCoordinates (String lat, String lng)
{
this.coord[0] = lat;
this.coord[1] = lng;
}
public String[] getCoordinates()
{
return coord;
}
}