|
(I know this is off topic, but I do not know where
to send it...)
Hello,
I would like to generate the XML from an existing DOM and send it to a file. How should I do it? I mean, having document = builder.newDocument(); and having add many elements to the document I want to generate a .xml file from it: <myxml> <element1> </element1> </myxml> ... Thanks in advance, Ram�n Talavera [EMAIL PROTECTED] public static void buildDom(AClass P) { if (G==null) return; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.newDocument(); // Create from whole cloth Element root = (Element) document.createElement("MyClass"); document.appendChild(root); Element author=(Element) document.createElement("author"); root.appendChild(author); Element fname=(Element) document.createElement("fname"); author.appendChild(fname); fname.appendChild( document.createTextNode(G.AuthorFName) ); Element sname=(Element) document.createElement("sname"); author.appendChild(sname); sname.appendChild( document.createTextNode(G.AuthorSName) ); // normalize text representation // getDocumentElement() returns the document's root node document.getDocumentElement().normalize(); } catch (ParserConfigurationException pce) { // Parser with specified options can't be built pce.printStackTrace(); } } // buildDom |
