How to create a XML document using JAVA(dom4j)


Developers can create a XML document using Java dom4j API by the following code;

(The XML file will be formatted)

Here is a Java class for storing the data for XML;

ServerInfo.java

package parametertoxmlexample;

/**
 *
 * @author tunat
 */
public class ServerInfo {
   
    private String IP;
    private String USERNAME;
    private String PASSWORD;
    private String DESCRIPTION;

    public ServerInfo(String ip,String username, String password, String description) {
        this.IP = ip;
        this.USERNAME = username;
        this.PASSWORD = password;
        this.DESCRIPTION = description;
    }

    /**
     * @return the IP
     */
    public String getIP() {
        return IP;
    }

    /**
     * @param IP the IP to set
     */
    public void setIP(String IP) {
        this.IP = IP;
    }

    /**
     * @return the USERNAME
     */
    public String getUSERNAME() {
        return USERNAME;
    }

    /**
     * @param USERNAME the USERNAME to set
     */
    public void setUSERNAME(String USERNAME) {
        this.USERNAME = USERNAME;
    }

    /**
     * @return the PASSWORD
     */
    public String getPASSWORD() {
        return PASSWORD;
    }

    /**
     * @param PASSWORD the PASSWORD to set
     */
    public void setPASSWORD(String PASSWORD) {
        this.PASSWORD = PASSWORD;
    }

    /**
     * @return the DESCRIPTION
     */
    public String getDESCRIPTION() {
        return DESCRIPTION;
    }

    /**
     * @param DESCRIPTION the DESCRIPTION to set
     */
    public void setDESCRIPTION(String DESCRIPTION) {
        this.DESCRIPTION = DESCRIPTION;
    }

}

Main.java class for creating XML file;

package parametertoxmlexample;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 *
 * @author tunatore
 */
public class Main {

    public static void main(String[] args) throws TransformerConfigurationException {

        ServerInfo server1 = new ServerInfo(“IP1”, “USERNAME1”, “PASSWORD1”, “DESCRIPTION1”);
        ServerInfo server2 = new ServerInfo(“IP1”, “USERNAME2”, “PASSWORD2”, “DESCRIPTION2”);
        ServerInfo server3 = new ServerInfo(“IP1”, “USERNAME3”, “PASSWORD3”, “DESCRIPTION3”);
        ServerInfo server4 = new ServerInfo(“IP1”, “USERNAME4”, “PASSWORD4”, “DESCRIPTION4”);
        ServerInfo server5 = new ServerInfo(“IP1”, “USERNAME5”, “PASSWORD5”, “DESCRIPTION5”);

        ListserverList = new ArrayList();
        serverList.add(server1);
        serverList.add(server2);
        serverList.add(server3);
        serverList.add(server4);
        serverList.add(server5);

        Document document = DocumentHelper.createDocument();
        Element SERVERCONFIG = document.addElement(“SERVERCONFIG”);
        Element SERVERS = SERVERCONFIG.addElement(“SERVERS”);
        for (Iterator<ServerInfo> it = serverList.iterator(); it.hasNext();) {
            ServerInfo serverInList = it.next();
            Element SERVER = SERVERS.addElement(“SERVER”);
            SERVER.addElement(“IP”).addText(serverInList.getIP());
            SERVER.addElement(“USERNAME”).addText(serverInList.getUSERNAME());
            SERVER.addElement(“PASSWORD”).addText(serverInList.getPASSWORD());
            SERVER.addElement(“DESCRIPTION”).addText(serverInList.getDESCRIPTION());
        }

        FormatXML(document, 1);
    }

    public static void FormatXML(Document document, int indentNumber) {
        try {
                Source xml = new StreamSource(new StringReader(document.asXML()));                
                StringWriter stringWriter = new StringWriter();
                StreamResult xmlOutput = new StreamResult(stringWriter);

                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                transformerFactory.setAttribute(“indent-number”, indentNumber);
                Transformer transformer = transformerFactory.newTransformer();
                transformer.setOutputProperty(OutputKeys.INDENT, “yes”);
                transformer.transform(xml, xmlOutput);
               
                System.out.print(xmlOutput.getWriter().toString());
               
        } catch (Exception ex) {
                System.out.print(ex.getMessage());
        }
    }
}

The output of the program;

run:
<?xml version=”1.0″ encoding=”UTF-8″?>
<SERVERCONFIG>
 <SERVERS>
  <SERVER>
   <IP>IP1</IP>
   <USERNAME>USERNAME1</USERNAME>
   <PASSWORD>PASSWORD1</PASSWORD>
   <DESCRIPTION>DESCRIPTION1</DESCRIPTION>
  </SERVER>
  <SERVER>
   <IP>IP1</IP>
   <USERNAME>USERNAME2</USERNAME>
   <PASSWORD>PASSWORD2</PASSWORD>
   <DESCRIPTION>DESCRIPTION2</DESCRIPTION>
  </SERVER>
  <SERVER>
   <IP>IP1</IP>
   <USERNAME>USERNAME3</USERNAME>
   <PASSWORD>PASSWORD3</PASSWORD>
   <DESCRIPTION>DESCRIPTION3</DESCRIPTION>
  </SERVER>
  <SERVER>
   <IP>IP1</IP>
   <USERNAME>USERNAME4</USERNAME>
   <PASSWORD>PASSWORD4</PASSWORD>
   <DESCRIPTION>DESCRIPTION4</DESCRIPTION>
  </SERVER>
  <SERVER>
   <IP>IP1</IP>
   <USERNAME>USERNAME5</USERNAME>
   <PASSWORD>PASSWORD5</PASSWORD>
   <DESCRIPTION>DESCRIPTION5</DESCRIPTION>
  </SERVER>
 </SERVERS>
</SERVERCONFIG>
BUILD SUCCESSFUL (total time: 6 seconds)

Download Source

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s