开发者

How to parse a XML returns from a GET (in Android)

url = new URL(url_str);
HttpURLConnection con开发者_开发百科nection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
InputStream stream = connection.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(stream);
doc.getDocumentElement().normalize();
Node node = doc.getElementsByTagName("baseName").item(0);
Log.d(TAG,">>>>"+node.getNodeValue());

When i try the "url_str" on web browser it gives me the following XML.

<files>
<file>
<fileId>441190</fileId>
<license>4</license>
<path>images</path>
<originalSource>
http://www.flickr.com/photos/stevendepolo/5165129565/sizes/m/
</originalSource>
<owner>Steven Depolo</owner>
<baseName>photo_5165129565_0ca9b101b0_t.jpg</baseName>
</file>
</files>

But for my output of "node.getNodeValue()" it returns null. But I need to get the string between baseName tag.


Try node.getTextContent(); instead of node.getNodeValue() this solved me most of the problems i had related to node value. sometimes the node value is not set instead the value is set to the text content.

Hope this would help.


Have you checked another fields? Does it returns the value for them? Please call getNodeType() on your node, what it returns? Consider using XPath instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜