Creating XML file in Java from array

In this post I describe you how creating XML file. XML will be contain data from array. Data will be present information about persons from company.

Let’s create XMLCreate project.

a1a2In tree of project You see XMLCreate main class.

a3For this project create class which will be getting data form array and write them into xml file. So cright click on project node and choose New->Java Class. In Class Name field write CreateNewXML and select from Package list our net.doraprojects package.

a4Click the Finish button. Create another class: Person in the same way as this before.


a6Into Person class paste this code:

package net.doraprojects;

public class Person 
    int id = 0;
    String firstname = null;
    String surname = null;
    String profession = null;
    public Person(int id, String firstname, String surname, String profession)
    { = id;  
     this.firstname = firstname;
     this.surname = surname;
     this.profession = profession;
    public int getId()
        return id;
     public String getFirstname()
        return firstname;
      public String getSurname()
        return surname;
       public String getProfession()
        return profession;



Into file paste this code:

package net.doraprojects;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class CreateNewXML 
    public CreateNewXML()
    Person person1 = new Person(12,"Kate","Muller","teacher");
    Person person2 = new Person(10,"Bill","Yung","musician");
    Person person3 = new Person(134,"Olaf","Roy","engineer");
    Person person4 = new Person(2,"Iga","Suly","shopkeeper");
    Person person5 = new Person(23,"Nancy","Caty","sailor");
    Person[] tab = new Person[] { person1, person2, person3, person4, person5 };
       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
       DocumentBuilder builder = factory.newDocumentBuilder();
       Document doc = builder.newDocument();
       Element rootElement = doc.createElement("employees");
       for(int i=0; i<tab.length;i++)
           Element personElement = doc.createElement("person");
           personElement.setAttribute("id", ""+tab[i].getId());
           Element fnameElement = doc.createElement("firstname");
           Element snameElement = doc.createElement("surname");
           Element professionElement = doc.createElement("profession");
       TransformerFactory transformerFactory = TransformerFactory.newInstance();
       Transformer transformer = transformerFactory.newTransformer();
       DOMSource dom = new DOMSource(doc);
       StreamResult result = new StreamResult(new File("employees.xml"));
       transformer.transform(dom, result);
       System.out.println("File employee was created.");
       catch(Exception e ){ System.out.println(e.getMessage()); }

a8aa8bAnd into main class paste this code:

package net.doraprojects;
public class XMLCreate {

    public static void main(String[] args) {
       new CreateNewXML();

a9It is tree of projects with all these files.

a10Run application. In your project Netbeans folder you see created new file: employees.xml.


a12If you add information about creating file, you see info looks like this: