如何使用 XStream(对于 Android Web 服务)从 XML 中仅检索(不是全部)特定元素?
在 Android 中从第三方 Web 服务解析 XML 文件的最佳方法是什么?
看来使用 XStream,我需要为 XML 中的所有内容定义类和属性。但我只想使用大约 5% 的 XML,XStream 中有快捷方式吗?
# Space.java
public class Space {
String id = null;
String name = null;
}
# Parsing Code
XStream xs = new XStream(new XppDomDriver());
xs.alias("space", Space.class);
Space s = (Space) xs.fromXML( myXMLData );
# space.xml
<space>
<can-join type="boolean">false</can-join>
<created-at type="datetime">2011-05-04T16:12:27-04:00</created-at>
<default-showpage>Source/SVN</default-showpage>
<description>Assembla Time Tracker for Android</description>
<id>dvpLtiDOOr4lSCeJe5cbCb</id>
<is-commercial type="boolean">true</is-commercial>
<is-manager type="boolean">true</is-manager>
<is-volunteer type="boolean">false</is-volunteer>
<name>Assemblandroid TimeTracker</name>
<parent-id nil="true"></parent-id>
<public-permissions type="integer">0</public-permissions>
<team-permissions type="integer">2</team-permissions>
<updated-at type="datetime">2011-05-04T16:12:27-04:00</updated-at>
<watcher-permissions type="integer">1</watcher-permissions>
<wiki-name>assemblandroid-timetracker</wiki-name>
</space>
What is the best way to parse an XML file from a Web Service from a third-party in Android ?
It seems that with XStream, I need to define classes and attributes for everything in the XML. But I only want to use like 5% of the XML, is there a shorcut for this in XStream?
# Space.java
public class Space {
String id = null;
String name = null;
}
# Parsing Code
XStream xs = new XStream(new XppDomDriver());
xs.alias("space", Space.class);
Space s = (Space) xs.fromXML( myXMLData );
# space.xml
<space>
<can-join type="boolean">false</can-join>
<created-at type="datetime">2011-05-04T16:12:27-04:00</created-at>
<default-showpage>Source/SVN</default-showpage>
<description>Assembla Time Tracker for Android</description>
<id>dvpLtiDOOr4lSCeJe5cbCb</id>
<is-commercial type="boolean">true</is-commercial>
<is-manager type="boolean">true</is-manager>
<is-volunteer type="boolean">false</is-volunteer>
<name>Assemblandroid TimeTracker</name>
<parent-id nil="true"></parent-id>
<public-permissions type="integer">0</public-permissions>
<team-permissions type="integer">2</team-permissions>
<updated-at type="datetime">2011-05-04T16:12:27-04:00</updated-at>
<watcher-permissions type="integer">1</watcher-permissions>
<wiki-name>assemblandroid-timetracker</wiki-name>
</space>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
SAXParser
和DefaultHandler
类。Use
SAXParser
andDefaultHandler
classes.