2012-08-15 4 views
0

안녕하세요 난이 이것 좀 봐 현재의 문제, 안드로이드

알고, 나는 내가 잘못 찾을 수 없습니다

여기

은 내 XML :

<?xml version="1.0" encoding="UTF-8"?> 
<menu> 
<item> 
    <name>Margherita</name> 
    <description>Single cheese topping</description> 
</item> 
<item> 
    <name>Double Cheese Margherita</name> 
    <description> 
     <![CDATA[ 
     <ul> 
      <li>item 1</li> 
      <li>item 2</li> 
      <li>item 3</li> 
     </ul> 
     <span>Loaded with Extra Cheese</span><br /> 
     ]]> 
    </description> 
</item> 
</menu> 

설명을 HTML 내용이 포함 된 String으로 가져와야합니다.

Adapter.java : 아니라 첫 번째 작품은 여기

내 코드는 ... <![CDATA[가 비어으로 <![CDATA[ 그러나 두 번째 없이 설명을 얻을 수 있습니다

XMLParser parser = new XMLParser(); 

    String xml = null; 
    try { 
     xml = parser.readTextFile(context.getAssets().open(FILENAME)); 
    } catch (IOException e1) { 
     e1.printStackTrace(); 
    } 

    // getting DOM element 
    Document doc = parser.getDomElement(xml); 
    // Extract items from DOM 
    NodeList nl = doc.getElementsByTagName(KEY_ITEM); 
    for (int i = 0; i < nl.getLength(); i++) { 
     Element e = (Element) nl.item(i); 
     Medoc m = new Medoc(); 
     // fill Medoc informations 
     m.setTitle(parser.getValue(e, KEY_NAME)); 
     m.setDescription(parser.getValue(e, KEY_DESC)); 
     // save in memory 
     data.add(m); 
    } 

saxParser.java :

public class XMLParser { 

// constructor 
public XMLParser() { 

} 

/** 
* Getting XML from URL making HTTP request 
* @param url string 
* */ 
public String getXmlFromUrl(String url) { 
    String xml = null; 

    try { 
     // defaultHttpClient 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 

     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     xml = EntityUtils.toString(httpEntity); 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    // return XML 
    return xml; 
} 

/** 
* Getting XML DOM element 
* @param XML string 
* */ 
public Document getDomElement(String xml) { 
    Document doc = null; 
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    try { 

     DocumentBuilder db = dbf.newDocumentBuilder(); 

     InputSource is = new InputSource(); 
      is.setCharacterStream(new StringReader(xml)); 
      doc = db.parse(is); 

     } catch (ParserConfigurationException e) { 
      Log.e("Error: ", e.getMessage()); 
      return null; 
     } catch (SAXException e) { 
      Log.e("Error: ", e.getMessage()); 
      return null; 
     } catch (IOException e) { 
      Log.e("Error: ", e.getMessage()); 
      return null; 
     } 

     return doc; 
} 

/** Getting node value 
    * @param elem element 
    */ 
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) 
        return child.getNodeValue(); 
       else if (child.getNodeType() == Node.CDATA_SECTION_NODE) 
        return child.getNodeValue(); 
      } 
     } 
    } 
    return ""; 
} 

/** 
    * Getting node value 
    * @param Element node 
    * @param key string 
    * */ 
public String getValue(Element item, String str) {  
     NodeList n = item.getElementsByTagName(str); 
     return this.getElementValue(n.item(0)); 
    } 

/** 
    * Getting XML String from stream 
    * @param Element isteam 
    * */ 
public String readTextFile(InputStream inputStream) { 
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 

    byte buf[] = new byte[1024]; 
    int len; 
    try { 
     while ((len = inputStream.read(buf)) != -1) { 
      outputStream.write(buf, 0, len); 
     } 
     outputStream.close(); 
     inputStream.close(); 
    } catch (IOException e) { 

    } 
    return outputStream.toString(); 
} 

} 

답변

0

여기에 문제가 있습니다

if (child.getNodeType() == Node.TEXT_NODE) { 
    return child.getNodeValue(); 
} 

CDATA 섹션은 텍스트 노드가 아닙니다. CDATA 섹션도 조회하려면 Node.TEXT_NODE 외에 Node.CDATA_SECTION_NODE을 찾아야합니다.