views:

1823

answers:

4

Hello, I want to write some text that contains whitespace characters such as newline and tab into an xml file so I use

Element element = xmldoc.createElement("TestElement");
element.appendChild(xmldoc.createCDATASection(somestring));

but when I read this back in using

Node vs =  xmldoc.getElementsByTagName("TestElement").item(0);
String x = vs.getFirstChild().getNodeValue();

I get a string that has no newlines anymore.
When i look directly into the xml on disk, the newlines seem preserved. so the problem occurs when reading in the xml file.

How can I preserve the newlines?

Thanks!

A: 

EDIT: cut all the irrelevant stuff

I'm curious to know what DOM implementation you're using, because it doesn't mirror the default behaviour of the one in a couple of JVMs I've tried (they ship with a Xerces impl). I'm also interested in what newline characters your document has.

I'm not sure if whether CDATA should preserve whitespace is a given. I suspect that there are many factors involved. Don't DTDs/schemas affect how whitespace is processed?

You could try using the xml:space="preserve" attribute.

McDowell
yes i know, that is why i use getFirstchild()
clamp
Uh! Missed that!
McDowell
thanks, where exactly should i add that xml:space="preserve" attribute? to the node that contains the text or to the xml root?
clamp
+1  A: 

You need to check the type of each node using node.getNodeType(). If the type is CDATA_SECTION_NODE, you need to concat the CDATA guards to node.getNodeValue.

fpmurphy
yes, the type of the node is CDATA. but what do you mean with concat CDATA guards?
clamp
+1  A: 

You don't necessarily have to use CDATA to preserve white space characters. The XML specification specify how to encode these characters.

So for example, if you have an element with value that contains new space you should encode it with

  


Carriage return:

 

And so forth

LiorH
thanks, but is there a way without encoding it? so that i can see the formatted text in the xml file itself?
clamp
+2  A: 

I don't know how you parse and write your document, but here's an enhanced code example based on yours:

// creating the document in-memory                                                        
Document xmldoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();

Element element = xmldoc.createElement("TestElement");                                    
xmldoc.appendChild(element);                                                              
element.appendChild(xmldoc.createCDATASection("first line\nsecond line\n"));              

// serializing the xml to a string                                                        
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();             

DOMImplementationLS impl =                                                                
    (DOMImplementationLS)registry.getDOMImplementation("LS");                             

LSSerializer writer = impl.createLSSerializer();                                          
String str = writer.writeToString(xmldoc);                                                

// printing the xml for verification of whitespace in cdata                               
System.out.println("--- XML ---");                                                        
System.out.println(str);                                                                  

// de-serializing the xml from the string                                                 
final Charset charset = Charset.forName("utf-16");                                        
final ByteArrayInputStream input = new ByteArrayInputStream(str.getBytes(charset));       
Document xmldoc2 = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(input);

Node vs =  xmldoc2.getElementsByTagName("TestElement").item(0);                           
final Node child = vs.getFirstChild();                                                    
String x = child.getNodeValue();                                                          

// print the value, yay!                                                                  
System.out.println("--- Node Text ---");                                                  
System.out.println(x);

The serialization using LSSerializer is the W3C way to do it (see here). The output is as expected, with line separators:

--- XML --- 
<?xml version="1.0" encoding="UTF-16"?>
<TestElement><![CDATA[first line
second line ]]></TestElement>
--- Node Text --- 
first line
second line
Aviad Ben Dov
thank you, i tried that but it doesnt work for me. while i can see the linebreaks are there in the xmlfile on disk, once i read them back with this code, they are gone. maybe my linebreak character is bad. how can i find out, which one it is?
clamp
The output I showed is a real output from my own machine of the code example I posted.Did you try writing the text with the code I suggested? Or only to read it using my code?Also, what is the encoding of your file (you can see that in my example, the encoding is UTF-16). I had a similar problem by not using the same encoding, and I fixed it by using Charset.forName() with the actual encoding used.
Aviad Ben Dov
yep, i did try your actual code in my case. i used exactly the same code to output the string. but it does not contain whitespaces. the encoding i use is encoding="ISO-8859-1" i will try to use UTF-16
clamp
If you use exactly the same code with ISO-8859-1, you will have problems - unless you change the Charset.forName to use ISO-8859-1.New-lines can be problematic between ASCII and UTF-16, so its worth a shot.
Aviad Ben Dov