android中的SAX解析器不返回值

时间:2012-06-03 17:14:03

标签: android sax saxparser

我在android中使用SAX解析器。问题是,在解析了我的本地xml文件后,我想检查它是否包含System.out.println的值,但它返回null。

所以,基本上如果我尝试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;
}
}

1 个答案:

答案 0 :(得分:0)

停止手动编写解析器

首先,Android提供“基础知识” - 并且 - 有可用的框架等。

这个简短的例子展示了如何使用android.sax框架而不是子类化DefaultHandler并花费不必要的时间来调试解析器。

class EntryParser {
    public static class Entry {
        public String mTitle;
        public String mDescription;
        public String mWebPage;
        public String mLatitude;
        public String mLongitude;
    }
    private List<Entry> mEntries = new ArrayList<EntryParser.Entry>();
    private Entry mEntry;
    public List<Entry> parse(String xml) throws SAXException {
        Xml.parse(xml, init());
        return Collections.unmodifiableList(mEntries);
    }
    public List<Entry> parse(InputStream xml) throws SAXException, IOException {
        Xml.parse(xml, Encoding.UTF_8, init());
        return Collections.unmodifiableList(mEntries);
    }
    private ContentHandler init() {
        mEntries.clear();
        RootElement nodes = new RootElement("nodes");
        Element entry = nodes.getChild("entry");
        entry.setElementListener(new ElementListener() {
            public void start(Attributes attributes) {
                mEntry = new Entry();
            }
            public void end() {
                // Validation goes here
                mEntries.add(mEntry);
            }
        });
        Element title = entry.getChild("title");
        title.setEndTextElementListener(new EndTextElementListener() {
            public void end(String body) {
                mEntry.mTitle = body;
            }
        });
        Element description = entry.getChild("description");
        description.setEndTextElementListener(new EndTextElementListener() {
            public void end(String body) {
                mEntry.mDescription = body;
            }
        });
        Element webpage = entry.getChild("webpage");
        webpage.setEndTextElementListener(new EndTextElementListener() {
            public void end(String body) {
                mEntry.mWebPage = body;
            }
        });
        Element coordinates = entry.getChild("coordinates");
        Element latitude = coordinates.requireChild("latitude");
        latitude.setEndTextElementListener(new EndTextElementListener() {
            public void end(String body) {
                mEntry.mLatitude = body;
            }
        });
        Element longitude = coordinates.requireChild("longitude");
        longitude.setEndTextElementListener(new EndTextElementListener() {
            public void end(String body) {
                mEntry.mLongitude = body;
            }
        });
        return nodes.getContentHandler();
    }
}

像这样调用:

EntryParser ep = new EntryParser();
List<EntryParser.Entry> entries = ep.parse(..); // String or InputStream