xmlpullparser(), whats wrong with my code?
Posted
by
Avinazz
on Stack Overflow
See other posts from Stack Overflow
or by Avinazz
Published on 2011-02-23T07:21:23Z
Indexed on
2011/02/23
7:25 UTC
Read the original article
Hit count: 260
I am expecting output to be an array list filled with all items as "thought" but instead, every time i add a value into arraylist, the previous value in list also gets update with new, hence resulting in duplicates. This behaviour goes on till end. Even though i have achieved the result by changing the approach, failure of my previous approach still bothers me. Any help or pointer will be really appreciated.
Below is xml snippet:
<Thoughts>
<country>
<name>India</name>
<item>
<itemId>1</itemId>
<itemDetailImage>detail.png</itemDetailImage>
<itemImage>image1.png</itemImage>
<itemDesc>Item Description 1</itemDesc>
<itemTitle>Item Title 1</itemTitle>
</item>
<item>
<itemId>2</itemId>
<itemDetailImage>detail2.png</itemDetailImage>
<itemImage>image2.png</itemImage>
<itemDesc>Item Description 2</itemDesc>
<itemTitle>Item Title 2</itemTitle>
</item>
<item>
<itemId>3</itemId>
<itemDetailImage>detail3.png</itemDetailImage>
<itemImage>image3.png</itemImage>
<itemDesc>Item Description 3</itemDesc>
<itemTitle>Item Title 3</itemTitle>
</item>
Below is a method for my trial and error:
private void printAll() throws XmlPullParserException, IOException{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser parser = factory.newPullParser();
URL url = new URL("http://192.168.1.3/DefaultXML.xml");
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
parser.setInput(is, null);
int eventType = parser.getEventType();
String name;
while(eventType != parser.END_DOCUMENT){
if(eventType == parser.START_DOCUMENT){
Log.i(TAG,"Start document");
}else if (eventType == parser.START_TAG ) {
name = parser.getName();
if(name.equalsIgnoreCase(NAME)){
this.cName = new Country();
this.cName.setName(parser.nextText());
}else if(name.equalsIgnoreCase(ITEM_DESC)){
this.thought.setItemDesc(parser.nextText());
}else if (name.equalsIgnoreCase(ITEM_ID)) {
this.thought.setItemID(parser.nextText());
}else if (name.equalsIgnoreCase(ITEM_DETAIL_IMAGE)) {
this.thought.setItemDetailImage(parser.nextText());
}else if (name.equalsIgnoreCase(ITEM_IMAGE)) {
this.thought.setItemImage(parser.nextText());
}else if (name.equalsIgnoreCase(ITEM_TITLE)) {
this.thought.setItemTitle(parser.nextText());
}
}else if (eventType == parser.END_TAG) {
name = parser.getName();
if(name.equalsIgnoreCase(ITEM)) {
this.cName.setThought(this.thought);
ctry.add(cName);
this.thought = new Thought();
}
}
eventType = parser.next();
}
//Log.i(TAG,"---" + ctry.toString());
}
© Stack Overflow or respective owner