Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't know how you parse and write your document, but here's an enhanced code example based on yours:</p> <pre><code>// 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); </code></pre> <p>The serialization using LSSerializer is the W3C way to do it (<a href="http://xerces.apache.org/xerces2-j/faq-dom.html#faq-3" rel="noreferrer">see here</a>). The output is as expected, with line separators:</p> <pre><code>--- XML --- &lt;?xml version="1.0" encoding="UTF-16"?&gt; &lt;TestElement&gt;&lt;![CDATA[first line second line ]]&gt;&lt;/TestElement&gt; --- Node Text --- first line second line </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload