bhaskarc
bhaskarc

Reputation: 9531

XML Pullparser: Getting The Value of a Particular node

Lets say I have a XML file:

<catalog>
   <book>
      <isbn>1</isbn>
      <author> A</author>
      <title>Title A</title>
      <description> Desc A</description>
   </book>
   <book>
      <isbn>2</isbn>
      <author>B</author>
      <title>Title B</title>
      <description>Desc B</description>
   </book>
  ...
<catalog>

As per Android's documentation I can fetch all the data in my Activity like this:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        stringXmlContent = getEventsFromAnXML(this);
        ...

}

private String getEventsFromAnXML(Activity activity)
            throws XmlPullParserException, IOException {
        StringBuffer stringBuffer = new StringBuffer();
        Resources res = activity.getResources();
        XmlResourceParser xpp = res.getXml(R.xml.myxmlfile  );
        xpp.next();
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {
                stringBuffer.append("\nSTART_TAG: " + xpp.getName());
            } else if (eventType == XmlPullParser.END_TAG) {
                stringBuffer.append("\nEND_TAG: " + xpp.getName());
            } else if (eventType == XmlPullParser.TEXT) {
                stringBuffer.append("\nTEXT: " + xpp.getText());
            }
            eventType = xpp.next();
        }

   return stringBuffer.toString();
    }

While this fetches all content from XML file, I am really struggling to filter it out for a certain condition.

For example let us say I want to get the author name for book with isbn of 2. How do I filter this data out using pullparser ?

Upvotes: 0

Views: 96

Answers (2)

Embattled Swag
Embattled Swag

Reputation: 1469

It requires a bit of guess and check (at least that's what I needed when I did it), but you can easily test a certain node's text to see if the ISBN is 2 (and, if it is, you can continue to parse the data. Otherwise, you can keep going until you get to the next ISBN node.

Upvotes: 1

Ankit Srivastava
Ankit Srivastava

Reputation: 364

check this out and modify according to your needs

  URL url2 = new URL("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=" +artist+              "&api_key=1732077d6772048ccc671c754061cb18");
    URLConnection connection = url2.openConnection();
    connection.setConnectTimeout(10000);
    connection.setReadTimeout(30000);

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();

    final Document document = db.parse(connection.getInputStream());
    document.getDocumentElement().normalize();
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xPathEvaluator = xPathfactory.newXPath();
    XPathExpression nameExpr = xPathEvaluator.compile("//lfm/artist/image");
    // XPathExpression nameExpr = xPathEvaluator.compile("//lfm/tracks/track/image");
    NodeList nl = (NodeList) nameExpr.evaluate(document, XPathConstants.NODESET);


    for (int zzz = 0; zzz < nl.getLength(); zzz++)
    {
        Node currentItem = nl.item(zzz);
        key = currentItem.getTextContent();


        // key = currentItem.getAttributes().getNamedItem("uri").getNodeValue();
    }

in the variable key,i get the value of the node which contains image

Upvotes: 1

Related Questions