Re: create a XML file



Nik wrote:

how can I create a XML file with java which looks like this when opened:

<archive name="archive1">
  <file name="xx">
    <size>100</size>
    <ext>jpg</ext>
  </file>
</archive>


thanks for help in advance!

http://java.sun.com/xml/tutorial_intro.html

Create a DOM document, and populate it's nodes.
Then save it as a file using code similar to this:

// Save the XML document
File f = File.createTempFile("an_xml_file", ".xml");
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(f);
transformer.transform(source, result);
// Done. (you'll need to handle some exceptions too).

--
Josef Garvi

"Reversing desertification through drought tolerant trees"
http://www.eden-foundation.org/

new income - better environment - more food - less poverty
.



Relevant Pages

  • Transformation of DOM
    ... I have a server app, which delivers data in a predefined xml format. ... data are build in a Dom document, which I serialize to a socketStream ...
    (comp.lang.java.help)
  • Parameters in stylesheets
    ... I set parameter for transformer, but actual output is not valid as expected. ... Scrap of code ... TransformerFactory tFactory = TransformerFactory.newInstance; ... Input xml ...
    (comp.lang.java.programmer)
  • Re: How to save DOM structure into XML file?
    ... public void transformDomToXml(Document doc,String fileName){ ... TransformerFactory tFactory = TransformerFactory.newInstance; ... Transformer transformer = tFactory.newTransformer; ... DOMSource source = new DOMSource; ...
    (comp.lang.java.programmer)