0
저는 안드로이드에서 SAX 파서로 작업하고 있습니다. 문제는 로컬 XML 파일을 구문 분석 한 후 값이 System.out.println
인 지 확인하려고하지만 null을 반환했습니다.안드로이드의 SAX 파서가 값을 반환하지 않습니다.
기본적으로 ArrayList<PlaceEntry> pl = PlacesXMLHandler.places;
을 시도한 다음 system.out.print(pl)
을 확인하면 빈 배열이됩니다. 나는 PlaceEntry
와 같은 것을하려고 노력했다. 그리고 그것은 나에게 또한 널을 줬다. 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;
}
}