tags:

views:

86

answers:

4

I have some data which my program discovers after observing a few things about files.

For instance, i know file name, time file was last changed, whether file is binary or ascii text, file content (assuming it is properties) and some other stuff.

i would like to store this data in XML format.

How would you go about doing it?

Please provide example.

+1  A: 

If you want something quick and relatively painless, use XStream, which lets you serialise Java Objects to and from XML. The tutorial contains some quick examples.

Catchwa
This is beyond awesome. Thank you!
mac
There are many XML binding solutions. JAXB (JSR-222) is the industry standard. An implementation is included in Java SE 6. Do a quick stackoverflow search for technology comparisons.
Blaise Doughan
A: 

You can use to do that SAX or DOM, review this link: http://articles.techrepublic.com.com/5100-10878_11-1044810.html I think is that you want

zoomer.hammerball
+1  A: 

Use StAX; it's so much easier than SAX or DOM to write an XML file (DOM is probably the easiest to read an XML file but requires you to have the whole thing in memory), and is built into Java SE 6.

A good demo is found here on p.2:

OutputStream out = new FileOutputStream("data.xml");
XMLOutputFactory factory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = factory.createXMLStreamWriter(out);
writer.writeStartDocument("ISO-8859-1", "1.0");
writer.writeStartElement("greeting");
writer.writeAttribute("id", "g1");
writer.writeCharacters("Hello StAX");
writer.writeEndDocument();
writer.flush();
writer.close();
out.close();
Jason S
A: 

Standard are the W3C libraries.

final Document docToSave = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
final Element fileInfo = docToSave.createElement("fileInfo");
docToSave.appendChild(fileInfo);
final Element fileName = docToSave.createElement("fileName");
fileName.setNodeValue("filename.bin");
fileInfo.appendChild(fileName);

return docToSave;

XML is almost never the easiest thing to do.

Vetsin