سؤال

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

هل كانت مفيدة؟

المحلول

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top