0

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 !!

3
  • 1
    This is a side comment, but why are you checking if child is an instance of CharacterData after you have already cast it. Isn't that a bit backwards? Regarding your actual question, I would recommend trying String content = cardNode.getTextContent().trim(); (without the .getLastChild()) and skip that whole CharacterData business. Commented Jan 20, 2014 at 5:47
  • Yeah just now observed it.. My careless mistake..I've corrected it Commented Jan 20, 2014 at 5:49
  • Thanks JLRise..I got it working like you said. But what I dont understand is - if we can get CDATA without using CharacterData, then why do people still sometimes use CharacterData ? Commented Jan 20, 2014 at 5:53

1 Answer 1

2

The reason your code was not working was that your card element has three child nodes:

  • A text node with a single space
  • The CDATA node with a bunch of text
  • A text node with a bunch of space

Your code was selecting the last child node (which was just blank space) to populate the content variable and the first node for the child variable, which was (a) not a CDATA node and (b) just empty space, and that explains the behavior you observed. The easy fix for this is to use the following to get content and skip all the CharacterData casting:

String content = cardNode.getTextContent().trim(); 

To answer your follow-up question, there may be times when people will want to be very precise and recognize a CDATA node as a CDATA node (and operate on separate text nodes individually), so the API provides capabilities to do this. But the .getTextContent() method allows getting the entire text content of an element, and that should do the job just fine in your case.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.