1

My XMLis

<ValidateUser>

     <userName>admin</userName>

     <password>admin</password>
 </ValidateUser>

My Main Activity is

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try{
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser parser = factory.newPullParser();                     
        InputStream input = getResources().openRawResource(R.raw.temp);
        parser.setInput(input,null);     
        int eventType = parser.getEventType();
        while(eventType != XmlPullParser.END_DOCUMENT){             
            if(eventType == XmlPullParser.START_DOCUMENT){ 
                String prefix = parser.getPrefix();
                String name   = parser.getName();
                Log.i("XML", String.format("prefix=%s,name=%s",prefix,name));

            }

            eventType = parser.next();
        }
    } catch (Exception e) {
        Log.e("XML","",e);
    }  

}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

My POJO class..

public class UserClass {

    public String username;
    public String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

}

I am still getting the tag name and prefix as null....previously my XML contained namespaces but nwow I have removed it.The xml is present in the raw folder inside the res folder

2
  • why do you need a pojo to parse xml Commented Aug 24, 2013 at 14:46
  • check the answer posted. i don't know why you need a POJO class. but try the below it should work Commented Aug 24, 2013 at 14:50

1 Answer 1

1

I don't see the need of a POJO class here. You have not parsed the tags also you have not used the POJO class any where.

More info @

http://developer.android.com/training/basics/network-ops/xml.html

If you are trying to follow the docs check the topic umder Parse XML

Considering you have a xml as below

<?xml version="1.0" encoding="utf-8"?>
<ValidateUser>
     <userName>admin</userName>
     <password>admin</password>
</ValidateUser>

Then

 InputStream open = ActivityName.this.getAssets().open("xmlname.xml");

If you have xm in raw folder

 InputStream open = getResources().openRawResource(R.raw.temp); 

Then

 XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        XmlPullParser xpp = factory.newPullParser();

        xpp.setInput(open, "UTF_8");

        boolean insideItem = false;
        // Returns the type of current event: START_TAG, END_TAG, etc..
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {

                if (xpp.getName().equalsIgnoreCase("ValidateUser")) {
                    insideItem = true;
                } else if (xpp.getName().equalsIgnoreCase("userName")) {
                    if (insideItem)
                        Log.i("....",xpp.nextText()); // extract the
                                                        // userName
                } else if (xpp.getName().equalsIgnoreCase("password")) {
                    if (insideItem)
                        Log.i("....",xpp.nextText());  // extract the passwrod

                }
            } else if (eventType == XmlPullParser.END_TAG
                    && xpp.getName().equalsIgnoreCase("ValidateUser")) {
                insideItem = false;
            }

            eventType = xpp.next(); // move to next element
        }

The Log

08-24 14:45:17.888: I/....(1108): admin
08-24 14:45:17.888: I/....(1108): admin
Sign up to request clarification or add additional context in comments.

6 Comments

error.. 08-24 20:36:32.957: W/System.err(22976): org.xmlpull.v1.XmlPullParserException: Unexpected token (position:unknown @1:2 in java.io.InputStreamReader@40f21258)
@user1901079 it works i tried it before posting it here. you should edit your post and post the full stack trace. i even posted the log. works fine if you do it right
i see the old post nothing new
I am getting the output with the xml in RAW folder
@user1901079 glad i could help follow the docs it has more info check the link posted in the answer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.