0
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>

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.