我在android中解析XML时遇到了困难。我有以下XML
<iq xmlns="jabber:client" type="result" to="blob@faisal-system/68bb97e7">
<album xmlns="naseebalbum">
<albumpicture>
<title>day1</title>
<creationdate>1397502000000</creationdate>
<picture>BASE64EncodedStringOfImage</picture>
</albumpicture>
<comments>
<comment>
<commentid>1</commentid>
<username>sana</username>
<text>i loved that pic</text>
<commenttime>1398264140000</commenttime>
</comment>
</comments>
<likes>
<like>
<likeid>4</likeid>
<username>sana</username>
<liketime>1398250919000</liketime>
</like>
</likes>
</album>
</iq>
有人可以帮我吗?
我想从Likes标签评论标签标签标签和图片标签中获取数据。
这是我一直试图做的事情。
public IQ parseIQ(XmlPullParser parser) throws Exception {
// TODO Auto-generated method stub
payload=""+parser.getText();
StringBuilder sb = new StringBuilder();
int depth = 1;
while (depth != 0) {
switch (parser.next()) {
case XmlPullParser.END_TAG:
depth--;
if (depth > 0) {
sb.append("</" + parser.getName() + ">");
}
break;
case XmlPullParser.START_TAG:
depth++;
StringBuilder attrs = new StringBuilder();
for (int i = 0; i < parser.getAttributeCount(); i++) {
attrs.append(parser.getAttributeName(i) + "=\""
+ parser.getAttributeValue(i) + "\" ");
}
sb.append("<" + parser.getName() + " " + attrs.toString() + ">");
break;
default:
sb.append(parser.getText());
break;
}
}
payload = sb.toString();
iq=new CustomIQ(payload);
iq.setType(Type.RESULT);
return iq;
}
答案 0 :(得分:1)
解析
public Void parse(InputStream is) {
try
{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(is, null);
xpp.nextTag();
xpp.require(XmlPullParser.START_TAG, null, "iq");
while (xpp.nextTag() == XmlPullParser.START_TAG) {
xpp.require(XmlPullParser.START_TAG, null, "album");
xpp.nextTag();
xpp.require(XmlPullParser.START_TAG, null, "albumpicture");
xpp.nextTag();
xpp.require(XmlPullParser.START_TAG, null, "title");
Log.i("title is....",""+xpp.nextText());
xpp.require(XmlPullParser.END_TAG, null, "title");
xpp.nextTag();
xpp.require(XmlPullParser.START_TAG, null, "creationdate");
Log.i("creation date i....",""+xpp.nextText());
xpp.require(XmlPullParser.END_TAG, null, "creationdate");
xpp.nextTag();
xpp.require(XmlPullParser.START_TAG, null, "picture");
Log.i("picture is....",""+xpp.nextText());
xpp.require(XmlPullParser.END_TAG, null, "picture");
xpp.nextTag();
xpp.require(XmlPullParser.END_TAG, null, "albumpicture");
xpp.nextTag();
xpp.require(XmlPullParser.START_TAG, null, "comments");
xpp.nextTag();
xpp.require(XmlPullParser.START_TAG, null, "comment");
xpp.nextTag();
xpp.require(XmlPullParser.START_TAG, null, "commentid");
Log.i("comment id is....",""+xpp.nextText());
xpp.require(XmlPullParser.END_TAG, null, "commentid");
xpp.nextTag();
xpp.require(XmlPullParser.START_TAG, null, "username");
Log.i("username is....",""+xpp.nextText());
xpp.require(XmlPullParser.END_TAG, null, "username");
xpp.nextTag();
xpp.require(XmlPullParser.START_TAG, null, "text");
Log.i("text is....",""+xpp.nextText());
xpp.require(XmlPullParser.END_TAG, null, "text");
xpp.nextTag();
xpp.require(XmlPullParser.START_TAG, null, "commenttime");
Log.i("comment is....",""+xpp.nextText());
xpp.require(XmlPullParser.END_TAG, null, "commenttime");
xpp.nextTag();
xpp.require(XmlPullParser.END_TAG, null, "comment");
xpp.nextTag();
xpp.require(XmlPullParser.END_TAG, null, "comments");
xpp.nextTag();
xpp.require(XmlPullParser.START_TAG, null, "likes");
xpp.nextTag();
xpp.require(XmlPullParser.START_TAG, null, "like");
xpp.nextTag();
xpp.require(XmlPullParser.START_TAG, null, "likeid");
Log.i("like id is....",""+xpp.nextText());
xpp.require(XmlPullParser.END_TAG, null, "likeid");
xpp.nextTag();
xpp.require(XmlPullParser.START_TAG, null, "username");
Log.i("username is....",""+xpp.nextText());
xpp.require(XmlPullParser.END_TAG, null, "username");
xpp.nextTag();
xpp.require(XmlPullParser.START_TAG, null, "liketime");
Log.i("liketime is....",""+xpp.nextText());
xpp.require(XmlPullParser.END_TAG, null, "liketime");
xpp.nextTag();
xpp.require(XmlPullParser.END_TAG, null, "like");
xpp.nextTag();
xpp.require(XmlPullParser.END_TAG, null, "likes");
xpp.nextTag();
xpp.require(XmlPullParser.END_TAG, null, "album");
}
xpp.require(XmlPullParser.END_TAG, null, "iq");
}catch(Exception e)
{
e.printStackTrace();
}
return null;
}
跳过标签
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;
}
}
}
日志
05-14 07:03:25.214: I/title is....(2212): day1
05-14 07:03:25.214: I/creation date i....(2212): 1397502000000
05-14 07:03:25.214: I/picture is....(2212): BASE64EncodedStringOfImage
05-14 07:03:25.214: I/comment id is....(2212): 1
05-14 07:03:25.224: I/username is....(2212): sana
05-14 07:03:25.224: I/text is....(2212): i loved that pic
05-14 07:03:25.224: I/comment is....(2212): 1398264140000
05-14 07:03:25.224: I/like id is....(2212): 4
05-14 07:03:25.224: I/username is....(2212): sana
05-14 07:03:25.224: I/liketime is....(2212): 1398250919000
阅读更多信息
http://developer.android.com/training/basics/network-ops/xml.html
答案 1 :(得分:0)
而不是为您提供实现案例的临时方法,只需查看DOM parser和XPath,它们将为您提供在Android或Java中解析XML的一般方法
答案 2 :(得分:0)
答案 3 :(得分:0)
https://github.com/Cruisoring/EasyXML提供了一种解析XML到地图的方法。
@Test
public void testDocument_mapOf() {
URL url = Thread.currentThread().getContextClassLoader()
.getResource("books.xml");
Document doc = EasySAXParser.parse(url);
List<? extends Map<String, String>> maps = doc.mapOf("book");
System.out.println(maps.get(0));
System.out.println(maps.get(1));
}
结果是12本书的12个地图,对于前面显示的前两个XML元素:
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<!-- <price>44.95</price> -->
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
打印出相应的前两张地图:
{author=Gambardella, Matthew, genre=Computer, description=An in-depth look at creating applications
with XML., id=bk101, title=XML Developer's Guide, publish_date=2000-10-01}
{author=Ralls, Kim, price=5.95, genre=Fantasy, description=A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen of the world., id=bk102, title=Midnight Rain, publish_date=2000-12-16}