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();
            }

            }
4

2 回答 2

1

你应该做类似的事情:

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();
}
于 2017-08-15T06:28:09.517 回答
0

如果我们假设 xml 格式正确,您可以更改代码以记住打开的标签、值以及标签结束时将值添加到字典/列表中,如下所示:

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();
}
于 2017-08-15T17:12:44.020 回答