2011-11-19 93 views
3

我解析服務器上的XML我讀它並解析它沒有任何錯誤但我無法看到數據。在android中解析CDATA

這裏是我的XML:

<BookData><book><Title><![CDATA[ABC]]></Title><AuthorFName1><![CDATA[A]]></AuthorFName1><AuthorLName1><![CDATA[B]]></AuthorLName1></book><book><Title><![CDATA[XYZ]]></Title><AuthorFName1><![CDATA[A]]></AuthorFName1><AuthorLName1><![CDATA[B]]></AuthorLName1></book> 

我使用DocumentBuilderFactory看到連我設置

dbf.setCoalescing(true); 

但仍無法正常工作,請參閱的DocumentBuilderFactory

Document doc = null; 
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
dbf.setCoalescing(true); 
try { 
    DocumentBuilder db = dbf.newDocumentBuilder(); 
    InputSource is = new InputSource(); 
    is.setCharacterStream(new StringReader(xml)); 
    doc = db.parse(is); 
} catch (ParserConfigurationException e) { 
    Log.d("XML parse Error:", e.getMessage()); 
    return null; 
} catch (SAXException e) { 
    Log.d("Wrong XML File Structure", e.getMessage()); 
    return null; 
} catch (IOException e) { 
    Log.d("IOException", e.getMessage()); 
    return null; 
} 
+0

在StackOverflow上有很多類似的問題。 http://stackoverflow.com/search?q=parse+cdata+in+android –

+0

請在這裏更新你的xml,因爲這個xml不是正確的。所以,所有未來的用戶都可以從這個答案中獲益。 –

+0

更新.............. –

回答

4
代碼的代碼

試試這個,你只需要將InputSource實例傳遞給這個方法就行了。

private void DOMParser(InputSource inputSource) { 

     DocumentBuilderFactory dBuilderFactory = DocumentBuilderFactory.newInstance(); 
     try { 
      DocumentBuilder documentBuilder = dBuilderFactory.newDocumentBuilder(); 
      Document dom = documentBuilder.parse(inputSource); 

      // get the root element..... 
      Element docElement = dom.getDocumentElement(); 
      Log.i("Root Element", docElement.getTagName()); 

      // now get the NodeList of root elements 
      NodeList nodeList = docElement.getElementsByTagName("book"); 
      Log.i("NodeList Length", nodeList.getLength()+""); 
      for (int i = 0; i < nodeList.getLength(); i++) { 

       Element eleBook = (Element) nodeList.item(i); 
       Log.i("Book Node", eleBook.getTagName()); 

       NodeList titleNode = eleBook.getElementsByTagName("Title"); 
       Element TitleEle = (Element) titleNode.item(0); 
       Log.i("Title", "Title - "+TitleEle.getFirstChild().getNodeValue()); 

       NodeList AuthorFName1Node = eleBook.getElementsByTagName("AuthorFName1"); 
       Element AuthorFName1Ele = (Element) AuthorFName1Node.item(0); 
       Log.i("AuthorFName1","AuthorFName1 - "+AuthorFName1Ele.getFirstChild().getNodeValue()); 

       NodeList AuthorFName11Node = eleBook.getElementsByTagName("AuthorLName1"); 
       Element AuthorFName11Ele = (Element) AuthorFName11Node.item(0); 
       Log.i("AuthorLName1","AuthorLName1 - "+AuthorFName11Ele.getFirstChild().getNodeValue()); 
      } 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
+0

感謝您的答案,但它在服務器上的打字錯誤沒有拼寫錯誤。這是我的打字錯誤。請建議我如何解析CDATA –

+0

現在你可以嘗試自己我也猜想有StackOverflow好友已經存在的問題已經丟失.. –

+0

而且,如果你不能解析XML你應該提供的問題是什麼是確切的問題你正面臨着。 –

6

這是樣本XML:

<?xml version="1.1" encoding="utf-8"?> 
<sample> 
    <artists> 
     <artist> 
      <artistname><![CDATA[+&&&Plus]]></artistname> 
     </artist> 
     <artist> 
      <artistname>015B</artistname>   
     </artist> 
    </artists> 
</sample> 

比方說,你已經在XML文件內容的xmlString,下面的方法將解析XML有或無CDATA標籤:

private static final String XML_TAG = "artist"; 
    private Object parseXML(String xmlString) throws Exception { 
    try { 
     Document doc = getDomElement(xmlString); 
     NodeList nodes = doc.getElementsByTagName(XML_TAG); 
     return retrieveData(doc,nodes); 
    } catch (Exception e) { 
     //Logger.logError(e); 
     throw e; 
    } 
} 

static public Document getDomElement(String xmlString) throws ParserConfigurationException, SAXException, IOException { 
    Document doc = null; 
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 

    DocumentBuilder db = dbf.newDocumentBuilder(); 
    InputSource is = new InputSource(); 
    is.setCharacterStream(new StringReader(xmlString)); 
    doc = db.parse(is); 


    return doc; 
} 


private Object retrieveData(Document doc, NodeList nodes) { 
    ArrayList<String> list = new ArrayList<String>(); 
    for(int i = 0 ; i < nodes.getLength() ; i++) { 
     Element element = (Element) nodes.item(i); 
     String name = getValue(element, "artistname"); 
     list.add(name); 
    } 
    return list; 
} 

static public String getValue(Element item, String str) { 
    NodeList n = item.getElementsByTagName(str); 
    return getElementValue(n.item(0)); 
} 

static public final String getElementValue(Node elem) { 
    try { 
     Node child; 
     if(elem != null){ 
      if (elem.hasChildNodes()){ 
       for(child = elem.getFirstChild(); child != null; child = child.getNextSibling()){ 
        if(child.getNodeType() == Node.CDATA_SECTION_NODE 
          || child.getNodeType() == Node.TEXT_NODE) 
        { 
         return child.getNodeValue().trim(); 
        } 
       } 
      } 
     } 
     return ""; 
    } catch (DOMException e) { 
     //Logger.logError(e); 
     return ""; 
    } 
} 
+0

+1幫助我很好的回答:) –