I have the following simple piece of code:
NodeList nodeList = document.getDocumentElement().getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node vtvRespNode = nodeList.item(i);
NodeList cardNodes = vtvRespNode.getChildNodes();
for (int j = 0; j < cardNodes.getLength(); j++) {
Node cardNode = cardNodes.item(j);
Card card = vtvResponse.new Card();
if (cardNode instanceof Element) {
String content = cardNode.getLastChild().getTextContent().trim();
if(cardNode.getNodeName().equals(CARD_TYPE)) {
Log.i(TAG,"cardType set for this card: "+content);
}
else if(cardNode.getNodeName().equals(TTL)) {
Log.i(TAG,"TTL set for this card: "+content);
}
else if(cardNode.getNodeName().equals(LOCAL_TIME_STAMP)) {
Log.i(TAG,"localtimeStamp set for this card: "+content);
}
else if(cardNode.getNodeName().equals(CARD_BLOB)) {
CharacterData child = (CharacterData) cardNode.getFirstChild();
if(child instanceof CharacterData){
Log.i(TAG,"cardNode is instanceof CharacterData");
content = child.getData();
}
Log.i(TAG,"blob set for this card: "+content);
}
}
}
}
Now, I have this sample xml:
<VtvResp>
<CI>
<localts> 1233546 </localts>
<ctype> 4 </ctype>
<ttl> 76542 </ttl>
<card> <![CDATA[{"timezone": 330.0, "date": "03/10/13", "windspeed": "15", "weather": "Partly Cloudy", "temperature": "28", "time": "04:02", "city_name": "Bangalore", "country": "India", "day": "thursday", "meridian": "pm"}]]>
</card>
</CI>
</VtvResp>
Here, I'm not able to get the CDATA data from the xml. The last Log of the else if blob always returns a blank string and I'm not able to figure out what I'm doing wrong.
Plz help !!
childis an instance ofCharacterDataafter you have already cast it. Isn't that a bit backwards? Regarding your actual question, I would recommend tryingString content = cardNode.getTextContent().trim();(without the.getLastChild()) and skip that wholeCharacterDatabusiness.