Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

CR/LF disappeared inside CDATA

  Asked By: Jose    Date: May 21    Category: Java    Views: 1134

I have a String that contains an XML message. The XML message
contains a CDATA element. Inside the CDATA, I have CR/LF as they are
(not escaped). I did a println and the CR/LF were still there. But
after I parased the string and did a node.getNodeValue, the CR/LF
disappeared. Can someone help!!!

This is how I created an inputStream from the original String.

import java.io.ByteArrayInputStream;


where xmlBody is the String that contains the XML message.

This ByteArrayInputStream was then passed to a document parser

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(is);
} catch (IOException ioe) {
logger.error("IOException " + ioe);
throw ioe;
} catch (ParserConfigurationException pce) {
logger.error("ParserConfigurationException " + pce);
throw pce;
} catch (SAXException ioe) {
logger.error("SAXException " + ioe);
throw ioe;

After I got to the correct node, that's how I retrieve the String.

if (n.getNodeType() == Node.CDATA_SECTION_NODE) {
return n.getNodeValue();



No Answers Found. Be the First, To Post Answer.

Didn't find what you were looking for? Find more on CR/LF disappeared inside CDATA Or get search suggestion and latest updates.