<![CDATA[ ]]> pour stocker une image
Bonjour, je voudrais stocker une image dans un fichier xml du style :
Citation:
<screenshot>
<author>blabla</author>
<image type="JPG|PNG|GIF"><![CDATA[Coontenu de l'image]]></image>
</screenshot>
Pour cela j'ai écris ces lignes :
Code:
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
|
// Création du document
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
// Auteur
Element author = document.createElement("author");
author.setTextContent( System.getProperty("user.name") );
// Tente de lire l'image
try {
//ImageInputStream imgis = ImageIO.createImageInputStream( imgpane.getImagePath() );
BufferedReader imgis = new BufferedReader( new FileReader( imgpane.getImagePath() ));
String line,content="";
StringBuffer buff = new StringBuffer();
while ( (line = imgis.readLine())!=null )
buff.append(line);
System.out.println(buff.toString());
CDATASection cdata = document.createCDATASection( buff.toString() );
image.appendChild(cdata);
} catch (IOException ex) {
ex.printStackTrace();
}
root.appendChild(author);
root.appendChild(image);
document.appendChild(root);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(new DOMSource(document), new StreamResult(System.out)); |
Mais la résultat n'est pas tout à fait celui attendu :
Citation:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<screenshot>
<author>moi</author>
<image><![CDATA["‰PNG]]>