0

I have a question that is similar to [this one here][1] I have read tutorials about XMLPullParser and do not seem to get this one.

I have an XML tag I want to parse using XMLPullParser

<dt>
: <sx>outcome</sx>
<sx>result</sx>
</dt>

I want to get the result ": outcome result." Because I am reading this XML from an online base. It may change. For instance :

<dt>
:degree or measure of
<d_link>succeeding</d_link>
</dt>

The question is how do I parse and get all the text in the tag "dt" irrespective of the name of the tags in it?

This is what I have tried but it is not working.

while (parser.next() != END_TAG){
            if (parser.getEventType() != TEXT)
            {
                continue;
            }
            else if (parser.getEventType() == TEXT)
            {
                Log.d("Text", parser.getText()+" in the likelihood");
                stringBuilder.append(parser.getText());
                parser.next();
            }

            }

2 Answers 2

1

You should do something similar to:

while (eventType != XmlPullParser.END_DOCUMENT) 
{
    String tagname = parser.getName();
    switch (eventType) 
    {
        case XmlPullParser.START_TAG:
            if (tagname.equalsIgnoreCase("dt")) 
            {
                 // create a new instance of DTClass
                 dt= new DTClass();
            }
            break;

        case XmlPullParser.TEXT:
             text = parser.getText();
             break;

         case XmlPullParser.END_TAG:
             if (tagname.equalsIgnoreCase("dt")) {
                  // add DTClass object to list
                  dts.add(dt);
             } 
             else if (tagname.equalsIgnoreCase("sx")) 
             {
                  dt.setSX(text);
             } 

             break;

         default:
             break;
     }
     eventType = parser.next();
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much Artur. The issue is I have no control of what the sub tag could be. It could be "sx" at this point for a particular query, it could be something else later. How do I handle this case?
Thanks Artur. I have another question similar to the above. How do I reach you please?
bestin-it.com find me on my blog
Got it. Thanks. Checking your blog now
0

If we assume that xml is properly formatted you can change the code to remember opened tag, value and when tag is ended add a value to the dictionary/list like below:

while (eventType != XmlPullParser.END_DOCUMENT) 
{
    String tagname = parser.getName();
    String detectedTag = "" ;
    String valueTag = "" ;
    switch (eventType) 
    {
        case XmlPullParser.START_TAG:
            if (tagname.equalsIgnoreCase("dt")) 
            {
                 // create a new instance of DTClass
                 // move this line to the end tag
                 //dt= new DTClass();
            }
            detectedTag = tagname ;
            break;

        case XmlPullParser.TEXT:
             valueTag = parser.getText();                 
             break;

         case XmlPullParser.END_TAG:
             if (tagname.equalsIgnoreCase(detectedTag)) {                      
                  // create a new instance of DTClass                      
                  dt= new DTClass();

                  // set value
                  dt.setValue(valueTag);

                  // add DTClass object to list od dictionary
                  dts.add(dt);
             } 
             else 
             {
                  // clear tag and value tag
                  detectedTag = "" ;
                  valueTag = "";
             } 

             break;

         default:
             break;
     }
     eventType = parser.next();
}

2 Comments

Thank you very much! It works now. Even the first answer you gave worked perfectly. Thanks.
Here you are 😊

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.