Java Language Writing to a XML File


Example

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");
serializer.setIndent(4);
serializer.write(doc);

Example

Code:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
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, 
            IOException{
        // 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
            firstNameElement.appendChild(person.getFirstName());
            lastNameElement.appendChild(person.getLastName());
            
            // add names to name
            nameElement.appendChild(firstNameElement);
            nameElement.appendChild(lastNameElement);
            
            // add value to age
            ageElement.appendChild(String.valueOf(person.getAge()));
            
            // add unit attribute to age
            ageElement.addAttribute(new Attribute("unit", person.getAgeUnit()));
            
            // add value to favColor
            favColorElement.appendChild(person.getFavoriteColor());
            
            // add all contents to person
            personElement.appendChild(nameElement);
            personElement.appendChild(ageElement);
            personElement.appendChild(favColorElement);
            
            // add person to root
            root.appendChild(personElement);
        }
        
        // 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()){
            file.createNewFile();
        }
        
        // 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");
        serializer.setIndent(4);
        serializer.write(doc);
    }

    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