[英]Android SimpleXML library for XML parsing
我正在使用SimpleXML庫解析一個長xml。 這是長XML的鏈接
http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topsongs/limit=10/xml
現在我想要有關im:image標簽的指導
我做了以下POJO課程
public class Image {
private int height;
public Image(@Attribute(name = "height") int height)
{
this.height=height;
}
@Attribute(name = "height")
public int getObjectHeight() {
return height;
}
}
但這對我來說並不正確,因為它只會處理高度...如何解析這些標簽之間的內容
<im:image height="170"> </im:image>
我的第二個問題是java中的變量名應該是什么...因為在Java中不允許im:image。
請盡快幫我。
謝謝
首先,您需要為元素添加名稱空間
@Namespace(reference = "http://itunes.apple.com/rss", prefix = "im")
public class Image {
@Element(name = "image ")
private String image_url;
@Attribute
private int height;
}
下一個代碼工作正常
@Root(name = "feed")
static class Feed {
@Element
Image image;
}
@Root(name = "image")
static class Image {
@Attribute(name = "height")
int height;
@Text
String content;
}
@Test
public void testPrefixedTag() throws Exception {
String xml =
"<feed xmlns:im=\"http://itunes.apple.com/rss\" xmlns=\"http://www.w3.org/2005/Atom\">" +
"<im:image height=\"170\">Content</im:image>" +
"</feed>";
Serializer serializer = new Persister();
Feed feed = serializer.read(Feed.class, xml);
}
因此,如果您在根標記中引用了im
命名空間(鏈接中的xml具有一個),則可以僅使用子標記的名稱而不使用它(在本例中為image
)。
要在標簽之間解析內容,請使用@Text
批注。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.