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 35 36 37 38 39 40 41 42 43 44 45
| try
{
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
//creating a new instance of a DOM to build a DOM tree.
Document doc = docBuilder.newDocument();
new AuthentificationServlet().createXml(doc);
out.println("<b> fichier xml enregistre </b>");
}
catch(Exception e)
{
System.out.println(e);
}
private void createXml(Document doc) throws TransformerException, IOException {
//creer un noeud
Element root = doc.createElement("configuration");
//ajouter un noeud après le dernier fils noeud
doc.appendChild(root);
Element child = doc.createElement("matricule");
root.appendChild(child);
Text text = doc.createTextNode("1234");
child.appendChild(text);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
//creer string from xml tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
String xmlString = sw.toString();
File file = new File("c:/newxml.xml");
BufferedWriter bw = new BufferedWriter (new OutputStreamWriter(new FileOutputStream(file)));
bw.write(xmlString);
bw.flush();
bw.close();
} |
Partager