gpt4 book ai didi

android - android 中的标准 Xml 解析器

转载 作者:太空狗 更新时间:2023-10-29 13:31:08 26 4
gpt4 key购买 nike

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;
}

最佳答案

            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。

关于android - android 中的标准 Xml 解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15332666/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com