2011-06-13 5 views
0
url = new URL(url_str); 
HttpURLConnection connection = (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()); 

웹 브라우저에서 "url_str"을 시도하면 다음 XML이 표시됩니다.XML에서 GET으로부터 파싱하는 방법 (Android)

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

"node.getNodeValue()"를 출력하는 경우 null을 반환합니다. 하지만 baseName 태그 사이에 문자열을 가져와야합니다.

답변

1

해보십시오 node.getTextContent(); node.getNodeValue() 대신 노드의 값과 관련된 문제를 해결했습니다. 값이 텍스트 내용으로 설정되는 대신 노드 값이 설정되지 않는 경우가 있습니다.

희망이 도움이 될 것입니다.

0

다른 입력란을 확인하셨습니까? 그것들에 대한 가치를 반환합니까? 노드에서 getNodeType()으로 문의하십시오. 반환되는 노드는 무엇입니까? 대신 XPath를 사용해보십시오.

관련 문제