import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import android.util.Log;

HashMap <String,String> hmTmpData = new  HashMap<String,String>();

StringBuilder html = new StringBuilder();

html.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?> \n");
html.append("<wid> \n");
html.append("  <header>                \n");
html.append("   <tm>201102101400</tm>  \n");
html.append("   <ts>4</ts>             \n");
html.append("   <x>102</x>             \n");
html.append("   <y>84</y>              \n");
html.append("  </header>               \n");
html.append(" <body> \n");  
html.append("   <data seq=\"0\">        \n");
html.append("    <hour>18</hour>        \n");
html.append("    <day>0</day>           \n");
html.append("    <temp>4.0</temp>       \n");
html.append("    <tmx>-999.0</tmx>      \n");
html.append("    <tmn>-999.0</tmn>      \n");
html.append("    <sky>4</sky>           \n");
html.append("    <pty>2</pty>           \n");
html.append("    <wfKor>눈/비</wfKor>   \n");
html.append("    <wfEn>Snow/Rain</wfEn> \n");
html.append("    <pop>60</pop>          \n");
html.append("    <r12>1.0</r12>         \n");
html.append("    <s12>0.5</s12>         \n");
html.append("    <ws>2.0</ws>           \n");
html.append("    <wd>0</wd>             \n");
html.append("    <wdKor>북</wdKor>       \n");
html.append("    <wdEn>N</wdEn>         \n");
html.append("    <reh>65</reh>          \n");
html.append("   </data>                 \n");
html.append(" </body> \n");
html.append("</wid> \n");

String xmlcontents = html.toString();

try {
 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 DocumentBuilder builder = factory.newDocumentBuilder();
 InputStream istream = new ByteArrayInputStream(xmlcontents.getBytes("utf-8"));
 Document doc = builder.parse(istream); 
 Element wid = doc.getDocumentElement(); 
 // #text 제거 ---------------------------------------------------------- 1
 NodeList textItems = wid.getElementsByTagName("#text");
 
 for(int i =0;i<textItems.getLength();i++){
   wid.removeChild(textItems.item(i));
 }
 NodeList bodyItems = wid.getElementsByTagName("body");
 Document bodyDoc = bodyItems.item(0).getOwnerDocument();   
 Element bodye = bodyDoc.getDocumentElement();    
 NodeList datas = bodye.getElementsByTagName("data");   
 NodeList dataitems = datas.item(0).getChildNodes();//-------------------------- 2

 String NodeName = "";
 String NodeValue = "";
 //로그에 찍어본다.---------------------------------------------------------- 3
 for(int i =0;i<dataitems.getLength();i++){     
   try {  
        Log.i("Test", "(bodyitems.item(i)).getNodeName()  : "+(dataitems.item(i)).getNodeName() );

        NodeName = (dataitems.item(i)).getNodeName();
        NodeValue = ((dataitems.item(i)).getFirstChild()).getNodeValue();   // 노드명이 #text  일때는 여기서 에러발생

        hmTmpData.put(NodeName, NodeValue);

        Log.i("Test", "(bodyitems.item(i)).getFirstChild().getNodeValue()  : "+((dataitems.item(i)).getFirstChild()).getNodeValue() );     
   } catch (Exception e) {
    // TODO: handle exception
   }
 }     
} catch (Exception e) {
 // TODO: handle exception
}          

기상청 날씨정보를 full파서로 파싱해 보았습니다. 
그런데, 2에서 처럼 NodeList 를 얻어 와서  3에서 처럼 로그를 뿌려보면.. 
존재하지도 않았던.. #text   라는 노드가 각 노드들 앞에 삽입되 있는 것을 볼 수 있는데요..
문제는 이 노드가   value 값을 갖지 않아서 인지  에러가 발생합니다.  그래서,
1에서와 같이 # text   노드 들을 삭제해 보려 했으나..  제거가 않되고 여전히 남아 있네요..
하여, # text   이놈을 삭제 할 수 있는 방법이 알고 싶습니다. 
또는  제가 위와 같은 처리에 있어서  #text에 대해 잘못 이해 하는 부분이 있다면 알려주시면 감사하겠습니다.


 

s/w 엔지니어, 개발자 천국을 지향합니다.