Reputation: 325
I have an xml file with this format:
<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;
<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
Thank you
Upvotes: 0
Views: 332
Reputation: 33515
Android : Parsing Pictures and data from XML File
So at first, if your datasource is placed on Internet you need to fetch it for next work. For this you can use HttpClient
with proper GET
request and then grap from response XML
.
Then you need to use some XML parser. You can use classic DOM Parser
to achieve your goal. First you need to get NodeList
of <items>
and then simply fetch next item's elements.
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.
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()
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
Upvotes: 1