Skip to content Skip to sidebar Skip to footer

Getting Specific Object Of Xml Which Can Be Parsed Using Xstream

I'm new to XML parsing and want to convert XML file resulting from a web service to list of POJOs. as i have done some work with JSon and found it very easy to work with. while in

Solution 1:

Alternatively you can use SAX parser for parsing xml.

Refer this example code

Solution 2:

At last found the way resembling to JSon . i hope this can help someone else. @parag this is what i was asking.........i appreciate your help but that was not what i was looking for..

package com.samjanz.xmlparsing;

import java.util.ArrayList;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.samjanz.xmlparsing.helpers.MyConverter;
import com.samjanz.xmlparsing.helpers.MyHTTPConnector;
import com.samjanz.xmlparsing.pojos.Calendar;
import com.thoughtworks.xstream.XStream;

publicclassXmlParsingActivityextendsActivity {

LinearLayout parent;
String serverResponce;
Document xmlDoc;
ArrayList<Calendar> calendarList;

/** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    parent = (LinearLayout) findViewById(R.id.parent);
    XStreamxstream=newXStream();
    xstream.alias("calendar", Calendar.class);

    calendarList = newArrayList<Calendar>();
    Stringurl= getText(R.string.calendar_url).toString();
    // get DOC
    xmlDoc = MyConverter.streamToDocument(MyHTTPConnector.UrlToStream(url));
    NodeListnodeLst= xmlDoc.getElementsByTagName("calendar");

    for (inti=0; i < nodeLst.getLength(); i++) {
        Nodenode= nodeLst.item(i);
        if (node != null) {
            Calendarobj= (Calendar) xstream.fromXML(MyConverter
                    .nodeToString(node));
            calendarList.add(obj);
            LayoutParamslparams=newLayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            TextViewtv=newTextView(this);
            tv.setLayoutParams(lparams);
            tv.setText("\n\nLoop No:" + (i + 1) + "\nId = "
                    + calendarList.get(i).getId() + "\nName = "
                    + calendarList.get(i).getName() + "\nExpiry Date = "
                    + calendarList.get(i).getExpiryDate()
                    + "\nThumbnail Url = "
                    + calendarList.get(i).getThumbnailUrl());
            parent.addView(tv);

        }

    }
}

}

Post a Comment for "Getting Specific Object Of Xml Which Can Be Parsed Using Xstream"