1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
....
ByteArrayOutputStream out = new ByteArrayOutputStream();
com.sun.org.apache.xml.internal.serialize.XMLSerializer serializer = getXMLSerializer(cdataNodes, bos);
// ObjectFactory ma_structure_xml -> contient mon arbre XML déjà initialisé
xmlParser.getMarshaller().marshal(ma_structure_xml, serializer.asContentHandler());
....
// Méthode générique de sérialisation
private com.sun.org.apache.xml.internal.serialize.XMLSerializer getXMLSerializer(String[] cDataElements, OutputStream cOut) {
// This code is from a sample online: http://jaxb.java.net/faq/JaxbCDATASample.java
// configure an OutputFormat to handle CDATA
com.sun.org.apache.xml.internal.serialize.OutputFormat of = new com.sun.org.apache.xml.internal.serialize.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(cDataElements); // example : "^baz" for node <baz>
// set any other options you'd like
of.setPreserveSpace(true);
of.setIndenting(true);
// create the serializer
com.sun.org.apache.xml.internal.serialize.XMLSerializer serializer = new com.sun.org.apache.xml.internal.serialize.XMLSerializer(of);
serializer.setOutputByteStream(cOut);
return serializer;
} |
Partager