Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

3 Steps to Writing and Reading XML Files

DZone's Guide to

3 Steps to Writing and Reading XML Files

If you're dealing with XML files, you're probably looking to marshal and unmarshal them. JAXB can lend a hand, making reading and writing XML files a three-step process.

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

To read or write XML files, you should consider JAXB (Java Architecture for XML Binding). JAXB will convert and reconvert XML objects using marshalling (writing to XML) and unmarshalling processes.

When and Where Do I Need to Write XML Files?

Creating a Custom Configuration or UI File

Suppose, in a project, you need to customize the User Data for each field or configure the system’s database to your standard.

Building a Generic XML File for Documentation and Installation

Alternatively, suppose you want to a make a configuration file for all users in order to access and process their data fields.

Your Task

Develop an XML configuration for all web users using their name, IP address, and location as their identifier.

Solution

Step one: Create the specified POJO with an 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;
    }

}


Note: We use the JAXB annotation for jaxbMarshaller.marshal jaxbMarshaller.unmarshal().

Now we write the created User POJO content to an 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 )

And lastly, 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();
        }
    }
}

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:
java ,xml ,marshalling and unmarshalling ,jaxb ,tutorial

Published at DZone with permission of Amuda Adeolu. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}