2013-08-23 43 views
1

我使用SimpleXML庫解析一個長xml。這裏是用於XML解析的Android SimpleXML庫

http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topsongs/limit=10/xml

現在我想關於指導IM 龍XML的鏈接:圖像標籤

我已經做了以下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中的變量名...因爲im:圖像不允許在java中。

請儘快幫我。

感謝

+0

你需要從xml鏈接中獲得什麼細節...... – Hariharan

回答

0

首先你需要添加命名空間的元素

@Namespace(reference = "http://itunes.apple.com/rss", prefix = "im") 
public class Image { 
    @Element(name = "image ") 
    private String image_url; 

    @Attribute 
    private int height; 
} 
+0

OP需要獲取元素的值。 – maress

0

下一個代碼工作正常

@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批註。