-->

How to get child or sub Tags in xml using XMLPullP

2019-07-05 05:10发布

问题:

I am using XmlPullParser in android for parsing an XML file. Its running fine when there are no sub tags inside my xml, I just check for the starting tag using XmlPullParse.START_TAG and get the appropriate attributes value, But I get stuck in a problem, here a single tag has another sub tag and in this sub tag there is an attribute which contains the image link. I am not able to extract that link from that sub tag.

Here is my XML :-

<section name="section1">
    <photo id="1" ilink="ImageLink 1"/>
    <photo id="2" ilink="ImageLink 2"/>    
</section>

<section name="section2">
    <photo id="3" ilink="ImageLink 1"/>
    <photo id="4" ilink="ImageLink 2"/>    
</section>

I am getting the parent tag that is "section" and its attribute that is "name" but how can I get "photo" tag according to section name ?? That is if I want to parse photo tag of section named "section2" then how can I do this ????

Please help me to sort out that. Any help would be appreciable.

Thanks in advance.

回答1:

You could write a xsl

<xsl:template match="/">
  <xsl:for-each select="section">
    <xsl:value-of select="concat('link ',photo@id, ' from ',@name,' is ',photo@ilink)"><xsl:value-of> 
  </xsl:for-each>
</xsl:template>

run the xsl on your xml, the output will be link 1 from section1 is ImageLink 1 .. link 4 from section2 is ImageLink 2



回答2:

This should work in your Android application.

MainActivity.java

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Sample
        SampleXMLPullParser.GetLinks("section2");
    }
}

SampleXMLPullParser.java

package com.example;

import java.io.StringReader;   
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

public class SampleXMLPullParser {

  public static void GetLinks (String section_name) {
    try {
      // Get the parser
      XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
      XmlPullParser xpp = factory.newPullParser();

      // XML data
      final String TAG_SECTION = "section";
      final String TAG_SECTION_ATTR_NAME = "name";
      final String TAG_PHOTO = "photo";
      final String TAG_PHOTO_ATTR_LINK = "ilink";
      final String inputXML = "<section name=\"section1\">"
                            +       "<photo id=\"1\" ilink=\"ImageLink 1\"/>"
                            +   "<photo id=\"2\" ilink=\"ImageLink 2\"/>"    
                            + "</section>"
                            + "<section name=\"section2\">"
                            +   "<photo id=\"3\" ilink=\"ImageLink 3\"/>"
                            +   "<photo id=\"4\" ilink=\"ImageLink 4\"/>"    
                            + "</section>";

      // Set the input
      xpp.setInput(new StringReader(inputXML));
      int eventType = xpp.getEventType();

      // Parser loop until end of the document
      boolean correctSection = false;
      while (eventType != XmlPullParser.END_DOCUMENT) {
        // Read the tag name
        String tagname = xpp.getName();

        // Check the event type
        if (eventType == XmlPullParser.START_TAG) {
          // Check 'section' tags
          if (tagname.equalsIgnoreCase(TAG_SECTION)) {
            // Opening tag, check the attribute
            String attrvalue = xpp.getAttributeValue(null, TAG_SECTION_ATTR_NAME);
            if (attrvalue.equals(section_name)) {
              // Section we're interested to
              correctSection = true;
            }
          }

          // Check 'photo' tags (only for the provided section)
          if (correctSection && tagname.equalsIgnoreCase(TAG_PHOTO)) {
            // Read the attribute and print on console
            String attrvalue = xpp.getAttributeValue(null, TAG_PHOTO_ATTR_LINK);
            System.out.println(attrvalue);
          }

        } else if (eventType == XmlPullParser.END_TAG) {
                       // Closing 'section' tag
                       if (correctSection && tagname.equalsIgnoreCase(TAG_SECTION))
                         correctSection = false;
        }

        // Move to next event
        eventType = xpp.next();
      }

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

This should print on the Debug output the two links that correspond to the section argument you're passing to the function.

Of course you can adapt it in the way you want.