안드로이드 개발 질문/답변
(글 수 45,052)
아래의 코드로 트위터의 xml을 파싱하는데,
xml의 본문이 영어로 된 것은 전부 이상없이 파싱하지만,
본문이 한글로 작성된 xml의 경우 글자의 갯수만큼 null 값이 나옵니다.
예를들면,
<text>안녕하세요. hello world</text>
를 파싱하면
nullnullnullnullnull. hello world
위와 같이 결과가 나타납니다.
이유가 뭔지 잘 모르겠네요..
답변 주시면 감사하겠습니다. ㅠㅠ
만우절 좋은 하루 보내세요
xml의 본문이 영어로 된 것은 전부 이상없이 파싱하지만,
본문이 한글로 작성된 xml의 경우 글자의 갯수만큼 null 값이 나옵니다.
예를들면,
<text>안녕하세요. hello world</text>
를 파싱하면
nullnullnullnullnull. hello world
위와 같이 결과가 나타납니다.
이유가 뭔지 잘 모르겠네요..
답변 주시면 감사하겠습니다. ㅠㅠ
만우절 좋은 하루 보내세요
// .... 중략 ....
article.clear();
InputStream in = httpArticleConnection.getInputStream();
DocumentBuilderFactory dbf;
dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse(in);
Element docEle = dom.getDocumentElement();
NodeList nl = docEle.getElementsByTagName("status");
if(nl != null && nl.getLength() > 0) {
for(int i = 0 ; i < nl.getLength() ; i++) {
Element status = (Element)nl.item(i);
Element sCreatedAt = (Element)status.getElementsByTagName("created_at").item(0);
Element sId = (Element)status.getElementsByTagName("id").item(0);
Element sArticle = (Element)status.getElementsByTagName("text").item(0);
Element sSource = (Element)status.getElementsByTagName("source").item(0);
Article a = new Article(getNodeValue(sCreatedAt), getNodeValue(sId), getNodeValue(sArticle), getNodeValue(sSource));
addNewArticle(a);
}
// ..... 중략
private String getNodeValue(Element ele) {
if(ele != null) {
Node temp = ele.getFirstChild();
StringBuilder sb = new StringBuilder();
while(temp != null) {
sb.append(temp.getNodeValue());
temp = temp.getNextSibling();
}
return sb.toString();
}
else {
return "";
}
}


