Noman Arain
Noman Arain

Reputation: 1162

Android rss unable to parse XML with attribute

Document doc = getDomElement(response); // getting DOM element
            NodeList nl = doc.getElementsByTagName(KEY_ITEM);
            // looping through all item nodes <item>
            for (int i = 0; i < nl.getLength(); i++) {
                Element e = (Element) nl.item(i);
                String name = getValue(e, KEY_NAME);
                String description = getValue(e, KEY_DESC);
                Log.e("description:", description);
            }

public String getValue(Element item, String str) {
    NodeList n = item.getElementsByTagName(str);
    return this.getElementValue(n.item(0));
}

public final String getElementValue(Node elem) {
    Node child;
    if (elem != null) {
        if (elem.hasChildNodes()) {
            for (child = elem.getFirstChild(); child != null; child = child.getNextSibling()) {
                if ((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.ELEMENT_NODE) ) {
                    return child.getNodeValue();
                }
            }
        }
    }
    return "";
}

In the above, the response is an XML rss feed and a child is below. What's happening is I am able to get the title, published, updated. But when I use getValue(e, "content") I get empty string. I would also like to get the Author name.

<entry>
  <title>Title1</title>
  <link rel="alternate" type="text/html" href="http://www.example.com" />
  <id>ID</id>

  <published>2012-09-08T18:45:40Z</published>
  <updated>2012-09-08T18:43:01Z</updated>
  <author>
      <name>Author name</name>
      <uri>http://www.example.com</uri>
  </author>
  <content type="html" xml:lang="en" xml:base="http://www.example.com/">
      &lt;p&gt;Test Test</content>
</entry>

Upvotes: 0

Views: 442

Answers (1)

Don Roby
Don Roby

Reputation: 41137

In the code

public final String getElementValue(Node elem) {
    Node child;
    if (elem != null) {
        if (elem.hasChildNodes()) {
            for (child = elem.getFirstChild(); child != null; child = child.getNextSibling()) {
                if ((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.ELEMENT_NODE) ) {
                    return child.getNodeValue();
                }
            }
        }
    }
    return "";
}

you are getting only the text from the first child text node. The contents can be split into multiple text nodes. You likely want to collect text from all of the child text nodes.

public final String getElementValue(Node elem) {
    Node child;
    StringBuilder sb = new StringBuilder();
    if (elem != null) {
        if (elem.hasChildNodes()) {
            for (child = elem.getFirstChild(); child != null; child = child.getNextSibling()) {
                if ((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.ELEMENT_NODE) ) {
                    sb.append(child.getNodeValue());
                }
            }
        }
    }
    return sb.toString();
}

To get the Author name value, you'll need to first step down another level in the hierarchy, as the "name" tag is nested inside the "author" tag. This will mean a bit of special handling as you traverse the top level nodes to locate the "author" node and then get its child "name" node.

Upvotes: 1

Related Questions