1

I have a colection of objects organised in trees that i want to save as a XML file, but i want to do this as optimally as i can. I use somehing like this (see the code) to do it, but i would appeciate if someone could tell me a better way of doing it :

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();


    Document doc = docBuilder.newDocument();
    Element rootElement = doc.createElement("company");
    doc.appendChild(rootElement);


    Element staff = doc.createElement("Staff");
    rootElement.appendChild(staff);


    Attr attr = doc.createAttribute("id");
    attr.setValue("1");
    staff.setAttributeNode(attr);


    Element firstname = doc.createElement("firstname");
    firstname.appendChild(doc.createTextNode("yong"));
    staff.appendChild(firstname);


    Element lastname = doc.createElement("lastname");
    lastname.appendChild(doc.createTextNode("mook kim"));
    staff.appendChild(lastname);


    Element nickname = doc.createElement("nickname");
    nickname.appendChild(doc.createTextNode("mkyong"));
    staff.appendChild(nickname);


    Element salary = doc.createElement("salary");
    salary.appendChild(doc.createTextNode("100000"));
    staff.appendChild(salary);


    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File("C:\\file.xml"));


    transformer.transform(source, result);

1 Answer 1

3

The shot answer is: use JAXB

In case of using it, you can put annotation such as @XmlElement on your fields and classes to serialize/deserialize them to XML seamlessly. And it will handle all the recursion in your tree without any problems.

4
  • +1 for JAXB, also note that JAXB has default rules for converting Java objects to XML so you only need to add annotations where you wish to specify non-default mappings: wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted
    – bdoughan
    Commented Dec 8, 2011 at 10:54
  • 2
    @Funtik - JAXB is really simple to use as well: blog.bdoughan.com/2010/10/how-does-jaxb-compare-to-simple.html
    – bdoughan
    Commented Dec 8, 2011 at 10:57
  • @Blaise Doughan +1 for the blog-post. My opinion is based on my previous bad experience with JAXB a couple of years ago. At that time I found Simple to be simpler and more efficient framework for XML transformations. Commented Dec 8, 2011 at 11:40
  • I don't really need simple, coding is not an issue; what i need is to use as little memory as possible because it's just a small part of a bigger project.
    – MRM
    Commented Dec 8, 2011 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.