Xml解析嵌套标记返回null

时间:2014-09-18 11:33:57

标签: java android xml xml-parsing

您好我是XML解析的新手,并完成了用于xml解析的Google教程。在他们使用的教程中:https://stackoverflow.com/feeds/tag?tagnames=android&sort=newest

所以我想用关于帖子的作者的信息来扩展它,我尝试了它但是它继续返回null,无论我在作者标签内使用什么(uri标签和名称标签)

我的文件在哪里"搜索标签"发生

/**
 * This class parses XML feeds from stackoverflow.com.
 * Given an InputStream representation of a feed, it returns a List of entries,
 * where each list element represents a single entry (post) in the XML feed.
 */
public class StackOverflowXmlParser {
    private static final String ns = null;

    // We don't use namespaces

    public List<Entry> parse(InputStream in) throws XmlPullParserException, IOException {
        try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in, null);
            parser.nextTag();
            return readFeed(parser);
        } finally {
            in.close();
        }
    }

    private List<Entry> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
        List<Entry> entries = new ArrayList<Entry>();

        parser.require(XmlPullParser.START_TAG, ns, "feed");
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            // Starts by looking for the entry tag
            if (name.equals("entry")) {
                entries.add(readEntry(parser));
            } 

            else {
                skip(parser);
            }
        }
        return entries;
    }

    // This class represents a single entry (post) in the XML feed.
    // It includes the data members "title," "link," and "summary."
    public static class Entry 
    {
       public final String rating;
       public final String title;
       public final String link;
       public final String author;

        private Entry(String rating, String title, String link, String author)         
        {
            this.rating = rating;    
            this.title = title;
            this.link = link;
            this.author = author;
        }        
    }


    // Parses the contents of an entry. If it encounters a title, summary, or link tag, hands them
    // off
    // to their respective &quot;read&quot; methods for processing. Otherwise, skips the tag.
    private Entry readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
        parser.require(XmlPullParser.START_TAG, ns, "entry");
        String rating = null;
        String title = null;
        String link = null;
        String author = null;



        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }

            String name = parser.getName();

            if (name.equals("re:rank")){
                rating = readRating_name(parser);
            } 

            else if (name.equals("title")){
                title = readTitle_name(parser);
            }

            else if (name.equals("id")){
                link = readLink_name(parser);
            }

            else if (name.equals("name")){
                author = readAuthor_name(parser);
            }

            else 
            {
                skip(parser);
            }
        }
        return new Entry(rating, title, link, author);
    }

    // Processes title tags in the feed.
    private String readRating_name(XmlPullParser parser) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "re:rank");
        String rating = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "re:rank");
        return rating;
    }   

    private String readTitle_name(XmlPullParser parser) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "title");
        String title = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "title");
        return title;
    } 

    private String readLink_name(XmlPullParser parser) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "id");
        String link = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "id");
        return link;
    } 

    private String readAuthor_name(XmlPullParser parser) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "name");
        String author = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "name");
        return author;
    }          

    // For the tags title and summary, extracts their text values.
    private String readText(XmlPullParser parser) 
            throws IOException, XmlPullParserException {
        String result = "";
        if (parser.next() == XmlPullParser.TEXT) {
            result = parser.getText();
            parser.nextTag();
        }
        return result;
    }

    // Skips tags the parser isn't interested in. Uses depth to handle nested tags. i.e.,
    // if the next tag after a START_TAG isn't a matching END_TAG, it keeps going until it
    // finds the matching END_TAG (as indicated by the value of "depth" being 0).
    private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            throw new IllegalStateException();
        }
        int depth = 1;
        while (depth != 0) {
            switch (parser.next()) {
            case XmlPullParser.END_TAG:
                    depth--;
                    break;
            case XmlPullParser.START_TAG:
                    depth++;
                    break;
            }
        }
    }
}

我输出解析结果的类:

// Uploads XML from stackoverflow.com, parses it, and combines it with
// HTML markup. Returns HTML string.
private String loadXmlFromNetwork(String urlString) throws XmlPullParserException, IOException 
{
    InputStream stream = null;
    StackOverflowXmlParser stackOverflowXmlParser = new StackOverflowXmlParser();
    List<Entry> entries = null;




    StringBuilder htmlString = new StringBuilder();         

    try {
        stream = downloadUrl(urlString);
        entries = stackOverflowXmlParser.parse(stream);
    // Makes sure that the InputStream is closed after the app is
    // finished using it.
    } 

    finally 
    {
        if (stream != null) 
        {
            stream.close();
        }
    }


    // Content section


    for (Entry entry : entries) 
    {
        // <a href="Link">Name link</a>

        String question = getString(R.string.question);
        String rating = getString(R.string.rating);
        String author = getString(R.string.author);

        // Question link + title

        htmlString.append("<p>" + question + "<a href='" + entry.link + "'>" + entry.title + "</a><br />");
        htmlString.append(rating + entry.rating + "<br />");
        htmlString.append(author + entry.author + "</p>");




    }
    return htmlString.toString();
}

2 个答案:

答案 0 :(得分:1)

希望它能帮到你,而不是xml解析使用这种方法来提供json数据[易于编码] ::

String xml = "xml here...";                
XMLSerializer xmlSerializer = new XMLSerializer(); 
JSON json = xmlSerializer.read( xml );

你可以获得json-lib - http://json-lib.sourceforge.net/index.html

答案 1 :(得分:0)

为了便于理解XML解析,请阅读本教程。 它真的很容易理解。 XML Parsing-Android Hive