简体   繁体   中英

XmlPullParser getAttributeValue always returns null

I'm trying to parse an xml file. But method getAttributeValue always returns null for me. First of all here's the xml file

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

And here's my code. I'm using AsyncTask

 public class XmlParser extends AsyncTask<Void, Void, String> {

        private XmlPullParserFactory xmlFactoryObject;
        private XmlPullParser myParser;
        private File file;
        FileInputStream fis;

        String value;

        public XmlParser() {
            try {
                xmlFactoryObject = XmlPullParserFactory.newInstance();
                myParser = xmlFactoryObject.newPullParser();

            } catch (XmlPullParserException e) {
                e.printStackTrace();
            }
        }

        public void setFile(File file1) {
            file = file1;
            execute();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            try {
                if(file == null){
                    Toast.makeText(getActivity(), "NULL", Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(getActivity(), "NOT NULL " + file.getName(), Toast.LENGTH_LONG).show();
                    fis = new FileInputStream(file);
                    myParser.setInput(fis, null);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            }
        }

        @Override
        protected String doInBackground(Void... params) {
            int event = 0;
            try {
                event = myParser.getEventType();
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            }
            while (event != XmlPullParser.END_DOCUMENT) {
                String name=myParser.getName();
                switch (event){
                    case XmlPullParser.START_TAG:
                        break;

                    case XmlPullParser.END_TAG:
                        if(name.equals("from")){
                            value = myParser.getAttributeValue(null, "value"); // this line always return null
                            Log.d(getTag(), "NEZINAU " + value);
                        }
                        break;
                }
                try {
                    event = myParser.next();
                } catch (XmlPullParserException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    Log.d(getTag(), "BAD " + e.getLocalizedMessage());
                }
            }
            return value;
        }

        @Override
        protected void onPostExecute(String aVoid) {
            super.onPostExecute(aVoid);
            System.out.println("myEND " + aVoid);
            Toast.makeText(getActivity(), "THE END!!! " + value, Toast.LENGTH_LONG).show();
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

this line value = myParser.getAttributeValue(null, "value"); // this line always return null value = myParser.getAttributeValue(null, "value"); // this line always return null always returns null. Why?

An attribute in XML looks like this:

<Tag attribute="value">content</Tag>

The tags in your XML document do not have attributes. You want to use xmlPullParser.getText() to get the text content between the opening and closing tag.

You must retrieve value with XmlPullParser.getText() on event XmlPullParser.TEXT. See example here: http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html

Try something like this:

    String currentName = "";
    while (event != XmlPullParser.END_DOCUMENT) {
        String name=myParser.getName();
        switch (event){
            case XmlPullParser.START_TAG:
                currentName = name;
                break;
            case XmlPullParser.TEXT:
                if("from".equals(currentName)){
                    value = myParser.getText(); 

                    Log.d(getTag(), "NEZINAU " + value);                        
                    }

                break;


            case XmlPullParser.END_TAG:
                break;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM