[英]Android : Parsing Pictures and data from XML File
I have an xml file with this format: 我有一个具有以下格式的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>
i want to parse these data and the picture link also and to put them in a listView with an adapter; 我也想解析这些数据和图片链接,并使用适配器将它们放在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>
the picture must takes the @+id/thumb ImageView,title takes @id/title TextView,pubdate takes @date TextView 图片必须使用@ + id / thumb ImageView,标题需要@ id / title TextView,pubdate需要@date TextView
Thank you 谢谢
Android : Parsing Pictures and data from XML File
Android:从XML文件解析图片和数据
So at first, if your datasource is placed on Internet you need to fetch it for next work. 因此,首先,如果您的数据源位于Internet上,则需要获取它以进行下一个工作。 For this you can use
HttpClient
with proper GET
request and then grap from response XML
. 为此,您可以将
HttpClient
与正确的GET
请求一起使用,然后从响应XML
。
Then you need to use some XML parser. 然后,您需要使用一些XML解析器。 You can use classic
DOM Parser
to achieve your goal. 您可以使用经典的
DOM Parser
来实现您的目标。 First you need to get NodeList
of <items>
and then simply fetch next item's elements. 首先,您需要获取
<items>
NodeList
,然后只需获取下一项的元素。
Pseudocode: 伪代码:
Get data from Internet: 从互联网获取数据:
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(<url>);
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
String xml = EntityUtils.toString(entity);
When you have XML as String you can create from its new DOM Document
and start parsing. 当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");
}
Helping method getElementValue() 帮助方法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 "";
}
And at the end if you are performing network operations you have to place your code into separated / background Thread and the best choice is 最后,如果要执行网络操作,则必须将代码放入单独的/后台线程中,最佳选择是
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.