Java Language Writing to a XML File


Writing to a XML File using XOM is very similar to reading it except in this case we are making the instances instead of retrieving them off the root.

To make a new Element use the constructor Element(String name). You will want to make a root element so that you can easily add it to a Document.

Element root = new Element("root");

The Element class has some handy methods for editing elements. They are listed below:

  • appendChild(String name) - this will basically set the value of the element to name.
  • appendChild(Node node) - this will make node the elements parent. (Elements are nodes so you can parse elements).
  • addAttribute(Attribute attribute) - will add an attribute to the element.

The Attribute class has a couple of different constructors. The simplest one is Attribute(String name, String value).

Once you have all of your elements add to your root element you can turn it into a Document. Document will take a Element as an argument in it's constructor.

You can use a Serializer to write your XML to a file. You will need to make a new output stream to parse in the constructor of Serializer.

FileOutputStream fileOutputStream = new FileOutputStream(file);
Serializer serializer = new Serializer(fileOutputStream, "UTF-8");



import nu.xom.Attribute;
import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Elements;
import nu.xom.ParsingException;
import nu.xom.Serializer;

public class XMLWriter{
    public static void main(String[] args) throws UnsupportedEncodingException, 
        // root element <example>
        Element root = new Element("example");
        // make a array of people to store
        Person[] people = {new Person("Smith", "Dan", "years", "green", 23), 
            new Person("Autry", "Bob", "months", "N/A", 3)};
        // add all the people
        for (Person person : people){
            // make the main person element <person>
            Element personElement = new Element("person");
            // make the name element and it's children: first and last
            Element nameElement = new Element("name");
            Element firstNameElement = new Element("first");
            Element lastNameElement = new Element("last");
            // make age element
            Element ageElement = new Element("age");
            // make favorite color element
            Element favColorElement = new Element("fav_color");
            // add value to names
            // add names to name
            // add value to age
            // add unit attribute to age
            ageElement.addAttribute(new Attribute("unit", person.getAgeUnit()));
            // add value to favColor
            // add all contents to person
            // add person to root
        // create doc off of root
        Document doc = new Document(root);
        // the file it will be stored in
        File file = new File("out.xml");
        if (!file.exists()){
        // get a file output stream ready
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        // use the serializer class to write it all
        Serializer serializer = new Serializer(fileOutputStream, "UTF-8");

    private static class Person {

        private String lName, fName, ageUnit, favColor;
        private int age;

        public Person(String lName, String fName, String ageUnit, String favColor, int age){
            this.lName = lName;
            this.fName = fName;
            this.age = age;
            this.ageUnit = ageUnit;
            this.favColor = favColor;

        public String getLastName() { return lName; }
        public String getFirstName() { return fName; }
        public String getAgeUnit() { return ageUnit; }
        public String getFavoriteColor() { return favColor; }
        public int getAge() { return age; }

This will be the contents of "out.xml":

xml file