Bonsoir la team,
je vous sollicite car je ne comprend pas comment utiliser la sérialisation sur mon exemple. Je vous explique :
Voici un exemple d'un fichier XML:
Pour avoir des accents dans mon fichier XML il me faut une balise CDATA pour avoir le fichier XML :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 <?xml version="1.0" encoding="UTF-8"?> <MON_FICHIER> <MA_BALISE>mes élements</MA_BALISE> </MON_FICHIER>
Pour cela, j'utilise le code suivant dans ma classe Java pour formatter ma structure XML:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 <?xml version="1.0" encoding="UTF-8"?> <MON_FICHIER> <MA_BALISE><![CDATA[mes élements]]></MA_BALISE> </MON_FICHIER>
J'ai trouvé ce code sur le net, mais je ne comprend pas 2 choses:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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; }
- 1) comment doit être utiliser OutputStream cOut en paramètre de la méthode getXMLSerializer ()?
- 2) comment ensuite récupérer un string à partir de la variable "serializer" après l'utilisation de getMarshaller ?
Pour celle ou celui qui a déjà utilisé ce genre de chose ça doit paraître évident, mais je bloque dessus depuis quelques temps.
Merci d'avance pour votre aide.
Berni.
Partager