0

我有一个这种格式的 xml 文件:

<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
<channel>
<item>
<cache>0</cache>
<id>v_article_16276.html</id>
<article_id>16276</article_id>
<type>article</type>
<img>
http://www.mywebsite.com/image.jpg
</img>
<datePub>
<![CDATA[ 25.03.2013 | 17h37 | Par Fabrice Antonio ]]>
</datePub>
</item>
</channel>
</rss>

我还想解析这些数据和图片链接,并将它们放在带有适配器的 listView 中;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="2dp" >

    <ImageView
        android:id="@+id/thumb"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:contentDescription="@string/app_name"
        android:src="@drawable/ic_launcher" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_toLeftOf="@+id/arrow"
        android:layout_toRightOf="@+id/thumb"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLines="2"
            android:text="@string/title"
            android:textSize="16sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginTop="5dp"
            android:padding="2dp"
            android:text="@string/date"
            android:textColor="#7F7F7F"
            android:textSize="10sp" />
    </RelativeLayout>

    <ImageView
        android:id="@+id/arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp"
        android:contentDescription="@string/app_name"
        android:src="@drawable/arrow_next" />

</RelativeLayout>

图片必须取@+id/thumb ImageView,title取@id/title TextView,pubdate取@date TextView

谢谢

4

1 回答 1

1

Android:从 XML 文件中解析图片和数据

因此,首先,如果您的数据源放置在 Internet 上,您需要获取它以进行下一步工作。为此,您可以使用HttpClient适当的GET请求,然后从响应中获取XML

然后你需要使用一些 XML 解析器。您可以使用经典DOM Parser来实现您的目标。首先,您需要获取然后简单地获取下一项的元素NodeList<items>

伪代码:

从互联网获取数据:

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(<url>);
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
String xml = EntityUtils.toString(entity);

当您将 XML 作为 String 时,您可以从它的新创建DOM Document并开始解析。

Document doc = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
doc = builder.parse(is);

NodeList nodeList = doc.getElementsByTagName("item");
for (int i = 0; i < nodeList.getLength(); i++) {
    Element e = (Element) nodeList.item(i);
    String img = getElementValue(e, "img");
    String pubDate = getElementValue(e, "datePub");
    String type = getElementValue(e, "type");
}

帮助方法 getElementValue()

public String getElementValue(Element e, String key) {
    NodeList nodes = e.getElementsByTagName(key);
    Node n = nodes.item(0);
    if (n != null) {
        return n.getFirstChild().getNodeValue();
    }
    return "";
}

笔记:

最后,如果您正在执行网络操作,则必须将代码放入单独的/后台线程中,最好的选择是

于 2013-03-25T18:11:35.657 回答