How to get child or helper tags in xml using XMLPullParsing in android

I am using XmlPullParser in android to parse an XML file. Its work is great when there are no subtets in my xml, I just check the start tag with XmlPullParse.START_TAG and get the corresponding attribute value, but I'm stuck in the problem, here one tag has a different subtag and this sub has an attribute that contains a link to picture. I cannot extract this link from this subtag.

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 get the parent tag, which is the โ€œsectionโ€ and its attribute โ€œnameโ€, but how can I get the tag โ€œphotoโ€ according to the name of the section? That is, if I want to analyze the photo library of a section called "section2", then how can I do this ??

Please help me figure this out. Any help would be noticeable.

Thanks in advance.

+3
source share
2 answers

You can write 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 xsl on your xml, the output will be link 1 from section 1 - ImageLink 1 .. link 4 from section 2 - ImageLink 2

+2
source

This should work in your Android app.

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 two links in the output of Debug that match the argument of the section you pass to the function.

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

+1
source

All Articles