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.