【问题标题】:Standard Xml parser in androidandroid中的标准XML解析器
【发布时间】:2013-03-11 06:44:22
【问题描述】:

android 中是否有标准的 xml 解析器,我可以在其中放置 RSS 的任何标准链接,它会给我结果吗?我找到了一个示例,但我必须指定项目的路径。 像这个链接这样的东西 http://www.rssboard.org/files/sample-rss-2.xml

 try {

        URL conn = new URL("http://www.rssboard.org/files/sample-rss-2.xml");
        XmlParserHelper helper = new XmlParserHelper(conn.openStream(), "rss");

        final String TITLES = "language";
        XmlParseObject object = helper.createObject(TITLES, "//channel//item//title");
        object.setParseAttributes();
        object.setParseContent();

        Map<String, List<XmlObject>> results = helper.parse();
        List<XmlObject> titlesList = results.get(TITLES);
        for (XmlObject title : titlesList) {
            Log.d("Guid:", title.toString());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }




   public class XmlParserHelper 
 {
protected String mRootName = "";
protected InputStream mXmlStream = null;
protected List<XmlParseObject> mParseList = new ArrayList<XmlParseObject>();

/**
 * Initialize xml helper with file stream
 * @param xmlStream input xml stream
 */
public XmlParserHelper(InputStream xmlStream, String rootName) 
{
    this.mXmlStream = xmlStream;
    this.mRootName = rootName;
}
/**
 * Point parse all attributes for XPath
 * @param objectName key for attributes list in response
 * @param XPath path to tag
 */
public void createAttributesObject(String objectName, String XPath) 
{
    XmlParseObject currentObject = new XmlParseObject(objectName, XPath);
    currentObject.setParseAttributes();
    mParseList.add(currentObject);
}
public void createContentObject(String objectName, String XPath) 
{
    XmlParseObject currentObject = new XmlParseObject(objectName, XPath);
    currentObject.setParseContent();
    mParseList.add(currentObject);
}
public XmlParseObject createObject(String objectName, String XPath) 
{
    XmlParseObject currentObject = new XmlParseObject(objectName, XPath);
    mParseList.add(currentObject);
    return currentObject;
}
public Map<String, List<XmlObject>> parse() throws Exception 
{
    if (mRootName.equals("")) 
    {
        throw new Exception("Root tag must be defined");
    }
    RootElement root = new RootElement(mRootName);
    for (XmlParseObject parselable : mParseList) 
    {
        parselable.configurateListenersForRoot(root);
    }
    try {
        Xml.parse(mXmlStream, Xml.Encoding.UTF_8, root.getContentHandler());
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    Map<String, List<XmlObject>> result = new HashMap<String, List<XmlObject>>();
    for (XmlParseObject parselable : mParseList) 
    {
        result.put(parselable.getName(), parselable.getResults());
    }
    return result;
}

}

【问题讨论】:

  • 能否提供您的代码?

标签: android xml-parsing standards


【解决方案1】:
            URL url = new URL("http://www.rssboard.org/files/sample-rss-2.xml");        
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(false);
            XmlPullParser xpp = factory.newPullParser();
            xpp.setInput(url.openConnection().getInputStream(), "UTF_8"); 
            //xpp.setInput(getInputStream(url), "UTF-8");

            boolean insideItem = false;

                // Returns the type of current event: START_TAG, END_TAG, etc..
            int eventType = xpp.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {

                    if (xpp.getName().equalsIgnoreCase("item")) {
                        insideItem = true;
                    } else if (xpp.getName().equalsIgnoreCase("title")) {
                        if (insideItem)
                            headlines.add(xpp.nextText()); //extract the headline
                    } else if (xpp.getName().equalsIgnoreCase("link")) {
                        if (insideItem)
                            links.add(xpp.nextText()); //extract the link of article
                    }
                }else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
                    insideItem=false;
                }

                eventType = xpp.next(); //move to next element
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

根据您的需要修改上述内容。我使用了 XmlPullParser。

【讨论】:

    猜你喜欢
    • 2012-10-01
    • 2023-03-26
    • 2011-12-08
    • 2012-10-20
    • 1970-01-01
    • 2012-05-21
    • 2012-01-12
    • 1970-01-01
    相关资源
    最近更新 更多