androidxml-parsingxmlpullparserandroid-pullparser

Performing XML parsing with XML Pull Parser in Android


I am trying XML parsing with XML Pull Parser. But I am getting only data for nametitle & name only. For rest of data I am getting Exception. Below I am posting my code.

Main Activity

public class PullParsingActivity extends Activity 
{
private static final String urL =
        "https://dl.dropbox.com/u/85981610/user.xml";

Button parseData;

private final String TAG = "PARSED DATA";
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    parseData = (Button)findViewById(R.id.parseData);

    parseData.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) 
        {
            try 
            {
                URL url = new URL(urL);
                url.openConnection();
                PullParser pullParser = new PullParser();
                pullParser.parse(url.openStream());
            }//try 
            catch (MalformedURLException e) 
            {
                Log.e("PullParsingActivity", e.getMessage());
            }//catch 
            catch (IOException e) 
            {
                Log.e("PullParsingActivity", e.getMessage());
            }//catch
        }//onClick
    });
}//onCreate
}//PullParsingActivity

Parser Class

public class PullParser 
{
private static final String ns = null;//set to null as we will not use namespace
Entry e1;

/*Method to parse data*/
public List<Entry> parse(InputStream is)
{
    XmlPullParser xpp = Xml.newPullParser();
    try 
    {
        xpp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        xpp.setInput(is, "UTF-8");
        /*Call next() & return event START_TAG or END_TAG*/
        xpp.nextTag();
        Log.i("PullParsing nextTag", ""+xpp.nextTag());
    }//try 
    catch (XmlPullParserException e) 
    {
        Log.e("PullParsing1", e.getMessage());
    }//catch 
    catch (IOException e) 
    {
        Log.e("PullParsing2", e.getMessage());
    }//catch
    finally
    {
        try 
        {
            is.close();
        }//try 
        catch (IOException e) 
        {
            Log.e("Pull Parsing3", e.getMessage());
        }//catch
    }//finally
    return readFeed(xpp);
}//parse

/*read data from xml*/
public List<Entry> readFeed(XmlPullParser parser) 
{
    List<Entry> parsedData = new ArrayList<Entry>();

    try 
    {
        /*checks whether current event type, namespace & name match*/
        parser.require(XmlPullParser.START_TAG, ns, "menu");
        while(parser.next() != XmlPullParser.END_TAG)
        {
            if(parser.getEventType() != XmlPullParser.START_TAG)
            {
                continue;
            }//if
            String name = parser.getName();

            /*look for the required tag*/
            if(name.equalsIgnoreCase("item"))
            {
                parsedData.add(readEntry(parser));
            }//if
            else
            {
                skip(parser);
            }//else
        }//while
    }//try 
    catch (XmlPullParserException e)
    {
        Log.e("PullParsing4", e.getMessage());
    }//catch 
    catch (IOException e) 
    {
        Log.e("PullParsing5", e.getMessage());
    }//catch
    return parsedData;
}//readFeed

public Entry readEntry(XmlPullParser parser)
{
    String nametitle = null;
    String name = null;
    String email = null;
    String mobile = null;
    String website = null;
    String jobtitle = null;
    String company = null;
    String businessphone = null;
    String homephone = null;
    String skype = null;
    String faxno = null;
    String businessaddress = null;
    String homeaddress = null;
    String otheraddress = null;

    try 
    {
        parser.require(XmlPullParser.START_TAG, ns, "item");

        while(parser.next() != XmlPullParser.END_TAG)
        {
            if(parser.getEventType() != XmlPullParser.START_TAG)
            {
                continue;
            }//if
            String parsedName = parser.getName();
            if(parsedName.equalsIgnoreCase("nametitle"))
            {
                nametitle = readNameTitle(parser);
            }//if
            else if (parsedName.equalsIgnoreCase("name")) 
            {
                name = readName(parser);
            }//else if
            else if (name.equalsIgnoreCase("email")) 
            {
                email = readEmail(parser);
            }//else if
            else if (name.equalsIgnoreCase("mobile")) 
            {
                mobile = readMobile(parser);
            }//else if
            else if (name.equalsIgnoreCase("website")) 
            {
                website = readWebsite(parser);
            }//else if
            else if (name.equalsIgnoreCase("jobtitle")) 
            {
                jobtitle = readJobTitle(parser);
            }//else if
            else if (name.equalsIgnoreCase("company")) 
            {
                company = readCompany(parser);
            }//else if
            else if (name.equalsIgnoreCase("businessphone")) 
            {
                businessphone = readBusinessPhone(parser);
            }//else if
            else if (name.equalsIgnoreCase("homephone")) 
            {
                homephone = readHomePhone(parser);
            }//else if
            else if (name.equalsIgnoreCase("skype")) 
            {
                skype = readSkype(parser);
            }//else if
            else if (name.equalsIgnoreCase("faxno")) 
            {
                faxno = readFaxNo(parser);
            }//else if
            else if (name.equalsIgnoreCase("businessaddress")) 
            {
                businessaddress = readBusinessAddress(parser);
            }//else if
            else if (name.equalsIgnoreCase("homeaddress")) 
            {
                homeaddress = readHomeAddress(parser);
            }//else if
            else if (name.equalsIgnoreCase("otheraddress")) 
            {
                otheraddress = readOtherAddress(parser);
            }//else if
            else 
            {
                skip(parser);
            }//else
        }//while
    }//try 
    catch (XmlPullParserException e) 
    {
        Log.e("Pull Parsing6", e.getMessage());
    }//catch 
    catch (IOException e) 
    {
        Log.e("Pull Parsing7", e.getMessage());
    }//catch

    e1 = new Entry(nametitle, name, email, mobile, website, jobtitle, 
            company, businessphone, homephone, skype, faxno, businessaddress, 
            homeaddress, otheraddress); 
    Log.d("PARSED DATA", e1.nametitle);
    Log.d("PARSED DATA", e1.name);
    //Log.d("PARSED DATA", e1.website);
    return e1;

}//readEntry

private String readFaxNo(XmlPullParser parser) 
{
    String faxno = null;
    try 
    {
        parser.require(XmlPullParser.START_TAG, ns, "faxno");
        faxno = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "faxno");
    }//try 
    catch (XmlPullParserException e) 
    {
        Log.e("Pull Parsing8", e.getMessage());
    }//catch 
    catch (IOException e) 
    {
        Log.e("Pull Parsing9", e.getMessage());
    }//catch
    return faxno;
}//readFaxNo



public String readNameTitle(XmlPullParser parser)
{
    String nameTitle = null;
    try 
    {
        parser.require(XmlPullParser.START_TAG, ns, "nametitle");
        nameTitle = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "nametitle");
    }//try 
    catch (XmlPullParserException e) 
    {
        Log.e("Pull Parsing8", e.getMessage());
    }//catch 
    catch (IOException e) 
    {
        Log.e("Pull Parsing9", e.getMessage());
    }//catch
    return nameTitle;
}//readTitle

public String readName(XmlPullParser parser) 
{
    String name = null;

    try 
    {
        parser.require(XmlPullParser.START_TAG, ns, "name");
        name = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "name");
    }//try 
    catch (XmlPullParserException e) 
    {
        Log.e("PullParsing10", e.getMessage());
    }//catch 
    catch (IOException e) 
    {
        Log.e("PullParsing11", e.getMessage());
    }//catch

    return name;
}//readLink

public String readEmail(XmlPullParser parser)
{
    String email = null;

    try 
    {
        parser.require(XmlPullParser.START_TAG, ns, "email");
        email = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "email");
    }//try 
    catch (XmlPullParserException e) 
    {
        Log.e("PullParsing12", e.getMessage());
    }//catch 
    catch (IOException e) 
    {
        Log.e("PullParsing13", e.getMessage());
    }//catch

    return email;
}//readEmail

public String readMobile(XmlPullParser parser)
{
    String mobile = null;
    try
    {
        parser.require(XmlPullParser.START_TAG, ns, "mobile");
        mobile = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "mobile");
    }//try
    catch(XmlPullParserException e)
    {
        Log.e("PullParsing 14", e.getMessage());
    }//catch
    catch (IOException e)
    {
        Log.e("PullParsing 15", e.getMessage());
    }//catch

    return mobile;
}//readMobile

public String readWebsite(XmlPullParser parser)
{
    String website = null;

    try 
    {
        parser.require(XmlPullParser.START_TAG, ns, "website");
        website = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "website");
    }//try 
    catch (XmlPullParserException e) 
    {
        Log.e("PullParsing16", e.getMessage());
    }//catch 
    catch (IOException e) 
    {
        Log.e("PullParsing17", e.getMessage());
    }//catch

    return website;
}//readWebsite

public String readJobTitle(XmlPullParser parser)
{
    String jobTitle = null;

    try 
    {
        parser.require(XmlPullParser.START_TAG, ns, "jobtitle");
        jobTitle = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "jobtitle");
    }//try 
    catch (XmlPullParserException e) 
    {
        Log.e("PullParsing18", e.getMessage());
    }//catch 
    catch (IOException e) 
    {
        Log.e("PullParsing19", e.getMessage());
    }//catch

    return jobTitle;
}//readEmail

public String readCompany(XmlPullParser parser)
{
    String company = null;

    try 
    {
        parser.require(XmlPullParser.START_TAG, ns, "comapny");
        company = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "company");
    }//try 
    catch (XmlPullParserException e) 
    {
        Log.e("PullParsing20", e.getMessage());
    }//catch 
    catch (IOException e) 
    {
        Log.e("PullParsing21", e.getMessage());
    }//catch

    return company;
}//readcompany

public String readBusinessPhone(XmlPullParser parser)
{
    String businessPhone = null;

    try 
    {
        parser.require(XmlPullParser.START_TAG, ns, "businessphone");
        businessPhone = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "businessphone");
    }//try 
    catch (XmlPullParserException e) 
    {
        Log.e("PullParsing22", e.getMessage());
    }//catch 
    catch (IOException e) 
    {
        Log.e("PullParsing23", e.getMessage());
    }//catch

    return businessPhone;
}//readBusinessPhone

public String readHomePhone(XmlPullParser parser)
{
    String homePhone = null;

    try 
    {
        parser.require(XmlPullParser.START_TAG, ns, "homephone");
        homePhone = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "homephone");
    }//try 
    catch (XmlPullParserException e) 
    {
        Log.e("PullParsing24", e.getMessage());
    }//catch 
    catch (IOException e) 
    {
        Log.e("PullParsing25", e.getMessage());
    }//catch

    return homePhone;
}//readEmail

public String readSkype(XmlPullParser parser)
{
    String skype = null;

    try 
    {
        parser.require(XmlPullParser.START_TAG, ns, "skype");
        skype = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "skype");
    }//try 
    catch (XmlPullParserException e) 
    {
        Log.e("PullParsing26", e.getMessage());
    }//catch 
    catch (IOException e) 
    {
        Log.e("PullParsing27", e.getMessage());
    }//catch

    return skype;
}//readSkype

public String readBusinessAddress(XmlPullParser parser)
{
    String businessAddress = null;

    try 
    {
        parser.require(XmlPullParser.START_TAG, ns, "businessaddress");
        businessAddress = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "businessaddress");
    }//try 
    catch (XmlPullParserException e) 
    {
        Log.e("PullParsing28", e.getMessage());
    }//catch 
    catch (IOException e) 
    {
        Log.e("PullParsing29", e.getMessage());
    }//catch

    return businessAddress;
}//readBusinessAddress

public String readHomeAddress(XmlPullParser parser)
{
    String homeAddress = null;

    try 
    {
        parser.require(XmlPullParser.START_TAG, ns, "homeaddress");
        homeAddress = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "homeaddress");
    }//try 
    catch (XmlPullParserException e) 
    {
        Log.e("PullParsing30", e.getMessage());
    }//catch 
    catch (IOException e) 
    {
        Log.e("PullParsing31", e.getMessage());
    }//catch

    return homeAddress;
}//readBusinessAddress

public String readOtherAddress(XmlPullParser parser)
{
    String otherAddress = null;

    try 
    {
        parser.require(XmlPullParser.START_TAG, ns, "otheraddress");
        otherAddress = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "otheraddress");
    }//try 
    catch (XmlPullParserException e) 
    {
        Log.e("PullParsing32", e.getMessage());
    }//catch 
    catch (IOException e) 
    {
        Log.e("PullParsing33", e.getMessage());
    }//catch

    return otherAddress;
}//readBusinessAddress




public void skip(XmlPullParser parser)
{
    try 
    {
        if(parser.getEventType() != XmlPullParser.START_TAG)
        {
            throw new IllegalStateException();
        }//if
        int depth = 1;
        while(depth != 0)
        {
            switch(parser.next())
            {
                case XmlPullParser.END_TAG:
                    depth--;
                    break;

                case XmlPullParser.START_TAG:
                    depth++;
                    break;
            }//switch
        }//while
    }//try 
    catch (XmlPullParserException e) 
    {
        Log.e("PullParsing34", e.getMessage());
    }//catch
    catch (IOException e) 
    {
        Log.e("PullParsing35", e.getMessage());
    }//catch
}//skip

private String readText(XmlPullParser parser) 
{
    String result = "";
    try 
    {
        if(parser.next() == XmlPullParser.TEXT)
        {
            result = parser.getText();
            parser.nextTag();
        }//if
    }//try 
    catch (XmlPullParserException e) 
    {
        Log.e("Pull Parsing36", e.getMessage());
    }//catch 
    catch (IOException e) 
    {
        Log.e("Pull Parsing37", e.getMessage());
    }//catch

    return result;
}

public static class Entry
{
    String nametitle;
    String name;
    String email;
    String mobile;
    String website;
    String jobtitle;
    String company;
    String businessphone;
    String homephone;
    String skype;
    String faxno;
    String businessaddress;
    String homeaddress;
    String otheraddress;

    public Entry(String nametitle, String name, String email, String mobile, String website,
            String jobtitle, String company, String businessphone, String homephone, 
            String skype, String faxno, String businessaddress, String homeaddress, 
            String otheraddress)
    {
        this.nametitle = nametitle;
        this.name = name;
        this.email = email;
        this.mobile = mobile;
        this.website = website;
        this.jobtitle = jobtitle;
        this.company = company;
        this.businessphone = businessphone;
        this.homephone = homephone;
        this.skype = skype;
        this.faxno = faxno;
        this.businessaddress = businessaddress;
        this.homeaddress = homeaddress;
        this.otheraddress = otheraddress;
    }//Constructor
}//Entry class
}//PullParser

XML File

<menu>
<item>
<nametitle>Mrs</nametitle>
<name>Maria Sharapova</name>
<email>maria@yahoo.com</email>
<mobile>9871234561</mobile>
<website>www.maria.com</website>
<jobtitle>software developer</jobtitle>
<company>Lata Softwares Pvt Ltd</company>
<businessphone>07442323232</businessphone>
<homephone>07441212121</homephone>
<skype>marai.sharapova</skype>
<faxno>07443434343</faxno>
<businessaddress>d-2 dadwara kota rajasthan</businessaddress>
<homeaddress>d-2 dadwara kota rajasthan</homeaddress>
<otheraddress>d-2 dadwara kota rajasthan</otheraddress>
</item>

<item>
<nametitle>Ms</nametitle>
<name>Sareena Williams</name>
<email>sareena@yahoo.com</email>
<mobile>9871212121</mobile>
<website>www.sareena.com</website>
<jobtitle>tenis player</jobtitle>
<company>Tenis organisation Pvt Ltd</company>
<businessphone>074423236565</businessphone>
<homephone>07441212178</homephone>
<skype>sareena.williams</skype>
<faxno>07443434387</faxno>
<businessaddress>d-3 dadwara kota rajasthan</businessaddress>
<homeaddress>d-3 dadwara kota rajasthan</homeaddress>
<otheraddress>d-3 dadwara kota rajasthan</otheraddress>
</item>
</menu>

This XML file I have uploaded in dropbox whose URL is as follows:

https://dl.dropbox.com/u/85981610/user.xml

The logcat details are as follows:

07-04 13:43:58.869: D/PARSED DATA(273): Mrs
07-04 13:43:58.869: D/PARSED DATA(273): Maria Sharapova
07-04 13:43:58.890: D/AndroidRuntime(273): Shutting down VM
07-04 13:43:58.890: W/dalvikvm(273): threadid=1: thread exiting with uncaught exception   (group=0x4001d800)
07-04 13:43:58.969: E/AndroidRuntime(273): FATAL EXCEPTION: main
07-04 13:43:58.969: E/AndroidRuntime(273): java.lang.NullPointerException: println  needs a message
07-04 13:43:58.969: E/AndroidRuntime(273):  at android.util.Log.println_native(Native Method)
07-04 13:43:58.969: E/AndroidRuntime(273):  at android.util.Log.d(Log.java:122)
07-04 13:43:58.969: E/AndroidRuntime(273):  at    com.android.pullparsing.PullParser.readEntry(PullParser.java:188)
07-04 13:43:58.969: E/AndroidRuntime(273):  at com.android.pullparsing.PullParser.readFeed(PullParser.java:76)
07-04 13:43:58.969: E/AndroidRuntime(273):  at com.android.pullparsing.PullParser.parse(PullParser.java:53)
07-04 13:43:58.969: E/AndroidRuntime(273):  at   com.android.pullparsing.PullParsingActivity$1.onClick(PullParsingActivity.java:37)
07-04 13:43:58.969: E/AndroidRuntime(273):  at android.view.View.performClick(View.java:2408)
07-04 13:43:58.969: E/AndroidRuntime(273):  at android.view.View$PerformClick.run(View.java:8816)
07-04 13:43:58.969: E/AndroidRuntime(273):  at android.os.Handler.handleCallback(Handler.java:587)
07-04 13:43:58.969: E/AndroidRuntime(273):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-04 13:43:58.969: E/AndroidRuntime(273):  at android.os.Looper.loop(Looper.java:123)
07-04 13:43:58.969: E/AndroidRuntime(273):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-04 13:43:58.969: E/AndroidRuntime(273):  at java.lang.reflect.Method.invokeNative(Native Method)
07-04 13:43:58.969: E/AndroidRuntime(273):  at java.lang.reflect.Method.invoke(Method.java:521)
07-04 13:43:58.969: E/AndroidRuntime(273):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-04 13:43:58.969: E/AndroidRuntime(273):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-04 13:43:58.969: E/AndroidRuntime(273):  at dalvik.system.NativeStart.main(Native  Method)

Solution

  • My app is running fine. Instead of Expat Pull parser, I used KXML Parser.