Writing and Reading XML File

                             Writing and Reading XML File

To read or write XML file , there is a great need for JAXB ( Java Archtecture for XML Binding )
.JAXB will convert and reconvert XML Object using Marshalling and UnMarshalling process

Writing to XML is called Marshalling

              When and where do I need to write XML File ?

1. Creating a custom confirguration or UI file

Example : Suppose in a project you need to custmize the User Data for each field or configure the system’s database to your standard

2. Building a generic XML file for documentation and installation

Example:

Suppose you want to a make a configuration file for all user inorder to access and process their data field
Task : Develop XML configuration for the all web user using their name, ip address and location as their identifier.

Solution

Step One ( Create the specified POJO with annotation  for the task )

package com.mytask.taska.personal;
import javax.xml.bind.annotation.*; // will be using XmlAttribute, XmlElement , XmlRootElement classes from this package
import java.util.*;// will be using Map and List classes from this package
@XmlRootElement
public class User
{
     Map<String,Long> uNameAndHashCode = new TreeMap<>();
     List userIpAddress = new LinkedList<>();

    public List getUserIpAddress()
    {
        return userIpAddress;
    }
    @XmlElement
    public void setUserIpAddress(List newIpAddress)
    {
        this.userIpAddress = newIpAddress;
    }
    public Map<String,Long> getuNameAndHashCode()
    {
        return uNameAndHashCode;
    }
    @XmlElement
    public void setuNameAndHashCode ( Map<String,Long> newUNameAndHcode) {
        this.uNameAndHashCode = newUNameAndHcode;
    }

}

N.O.T.E , we use JAXB annotation to make working and using jaxbMarshaller.marshal

,jaxbMarshaller.unmarshal() to perfrom the XML object

Step Two ( Write the created User POJO content to XML file  )
package com.mytask.taska.personal;
import java.io.*; // will be using only File class from this package
import javax.xml.bind.*; // will be using JAXBContext,Marshaller and JAXBException classes from this package
public class UserXML {
    public static void main(String[] args) {
        User user = new User();
        user.uNameAndHashCode.put("Amuda Adeolu Badmus", (long) 892382323.23323233);
        user.uNameAndHashCode.put("Vipin Jaiswal", (long) 323783292.32323);
        user.setuNameAndHashCode(user.uNameAndHashCode);

        user.userIpAddress.add("10.199.212.2");
        user.userIpAddress.add("10.232.012.0");
        user.setUserIpAddress(user.userIpAddress);
        try {
            File file = new File("C:\\Users\\User\\Music\\userConfigData.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(User.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            jaxbMarshaller.marshal(user, file);
            jaxbMarshaller.marshal(user, System.out);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}


....
JAXB ( Marshalling , Un Mashalling )
JAXB ( Marshalling )

Reading from XML is called Un-Marshalling

Step Three ( Read the created UserXML File  for the task )
package com.mytask.taska.personal;
import java.io.*; // will be using only File class from this package
import javax.xml.bind.*; // will be using JAXBContext,UnMarshaller and JAXBException classes from this package
public class ReadUserXML {
    public static void main(String[] args) {
        try {
            File file = new File("C:\\Users\\User\\Music\\userConfigData.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(User.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            User myUser = (User) jaxbUnmarshaller.unmarshal(file);
            System.out.println(myUser);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}
Advertisements

2 thoughts on “Writing and Reading XML File

Add yours

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

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: