Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is the code sample referenced by the site mentioned above:</p> <pre><code>import java.io.File; import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.apache.xml.serialize.OutputFormat; import org.apache.xml.serialize.XMLSerializer; import org.w3c.dom.Document; public class JaxbCDATASample { public static void main(String[] args) throws Exception { // unmarshal a doc JAXBContext jc = JAXBContext.newInstance("..."); Unmarshaller u = jc.createUnmarshaller(); Object o = u.unmarshal(...); // create a JAXB marshaller Marshaller m = jc.createMarshaller(); // get an Apache XMLSerializer configured to generate CDATA XMLSerializer serializer = getXMLSerializer(); // marshal using the Apache XMLSerializer m.marshal(o, serializer.asContentHandler()); } private static XMLSerializer getXMLSerializer() { // configure an OutputFormat to handle CDATA OutputFormat of = new OutputFormat(); // specify which of your elements you want to be handled as CDATA. // The use of the '^' between the namespaceURI and the localname // seems to be an implementation detail of the xerces code. // When processing xml that doesn't use namespaces, simply omit the // namespace prefix as shown in the third CDataElement below. of.setCDataElements( new String[] { "ns1^foo", // &lt;ns1:foo&gt; "ns2^bar", // &lt;ns2:bar&gt; "^baz" }); // &lt;baz&gt; // set any other options you'd like of.setPreserveSpace(true); of.setIndenting(true); // create the serializer XMLSerializer serializer = new XMLSerializer(of); serializer.setOutputByteStream(System.out); return serializer; } } </code></pre>
 

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