Saurabh Verma
Saurabh Verma

Reputation: 6728

Unable to get data from CDATA in Java

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

Upvotes: 0

Views: 1260

Answers (1)

JLRishe
JLRishe

Reputation: 101748

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.

Upvotes: 2

Related Questions