0

I have the following XML:

<ex>

    <email>
        [email protected]
    </email>

    <login_status>
        TRUE
    </login_status>

    <category>
        Personal
    </category>

    <subcategory>
        Food
    </subcategory>

    <May>
        50
    </May>

</ex>

I have the following Java Code:

XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance();
                            XmlPullParser myParser = xmlFactoryObject.newPullParser();
                            myParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
                            InputStream inputstream = new ByteArrayInputStream(response.getBytes());
                            myParser.setInput(inputstream, null);

                            int event;
                            String text = null;
                            try {
                                event = myParser.getEventType();
                                while (event != XmlPullParser.END_DOCUMENT) {
                                    String name = myParser.getName();
                                    switch (event) {
                                        case XmlPullParser.START_TAG:
                                            break;
                                        case XmlPullParser.TEXT:
                                            text = myParser.getText();
                                            System.out.println("text"+text);
                                            break;

                                        case XmlPullParser.END_TAG:
                                            String email = "email";
                                        if(name.equals("email")){
                                            email = myParser.getAttributeValue(null,"email");
                                            System.out.println("emaildecrypt"+email);
                                        }
                                            break;
                                    }
                                    event = myParser.next();

                                }
                                //   parsingComplete = false;
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

However I am always getting Null when I print the email, I am following this tutorial but I am not able to determine where things go wrong? Any hints?

1 Answer 1

2

You need to use getText instead of getAttributeValue :

email = myParser.getText();                                             
System.out.println("emaildecrypt"+email);

This will return the text in the current tag but be sure to capture it in the case XmlPullParser.TEXT (as you mentioned in the comment) but also keep the tag name so you will know where you are:

int event;
String text = null;

try {
    event = myParser.getEventType();
    String name = ""
    while (event != XmlPullParser.END_DOCUMENT) {       
        switch (event) {
        case XmlPullParser.START_TAG:
            name = myParser.getName();
            break;
        case XmlPullParser.TEXT:
            if(name.equals("email")){
                text = myParser.getText();
                System.out.println("text"+text);
            }           
            break;

        case XmlPullParser.END_TAG:         
            break;
        }
        event = myParser.next();

    }
    //   parsingComplete = false;
} catch (Exception e) {
    e.printStackTrace();
} 

getAttributeValue will return an attribute value for the current tag. if email tag was defined as:

<email address="[email protected]">

Then to get the address value you would use (for the email tag only):

myParser.getAttributeValue(null,"address");
Sign up to request clarification or add additional context in comments.

6 Comments

Nope - all is null but when I do case XmlPullParser.TEXT: text = myParser.getText(); System.out.println("text"+text); I get the values.
yes, ofcourse you are checking the text at the tag end!. adding the text case will fix it. you need to keep the context though of what tag you are currently processing so you will know to which variable you need to assign the text value
Its very strange if(name.equals("email")){ If I this shift to the XmlPullParser.TEXT case I get a null on name, whereas I have already defined name.
look at my edited answer. use the name to keep the tag name on a tag start and use it in the text case
The only problem with this is that it loops twice, for the first time I get the email second loop its empty and overrides the previous value. The current system.out results in: 05-26 14:54:03.977 22213-22263/exa.com.ex I/System.out﹕ [email protected] 05-26 14:54:03.977 22213-22263/exa.com.ex I/System.out﹕ email
|

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.