Over a million developers have joined DZone.

Hello World with Java Architecture for XML Binding

DZone's Guide to

Hello World with Java Architecture for XML Binding

· Java Zone
Free Resource

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

JAXB (Java Architecture for XML Binding) provides functionality to read or write XML from a file. Since XML is used everywhere in Java, this is useful. Here I'm assuming you have some knowledge on XML.

What you should have

1. JAXB 

Download it and add its bin  folder to your classpath. I have used Eclipse as the IDE.

Let's start...

1. Create a project in Eclipse called JAXB.

2. Create the library directory and copy all the JAXB jars there. (lets call its "lib")

3. Add lib to the build path.

4. You should have an XML schema definition file, so let's create one. Let's name it as GSCProfile.xsd

5. Copy following content to the schema file.


<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/GSCProfile" xmlns:tns="http://www.example.org/GSCProfile" elementFormDefault="qualified">

<complexType name="GSC">
<element name="Name" type="string"></element>
<element name="Rating" type="int"></element>

<complexType name="Profile">
<element name="ProfileName" type="string"></element>
<element name="GSCElements" type="tns:GSC" maxOccurs="14" minOccurs="14"></element>

<complexType name="GSCProfiles">
<element name="Profile" type="tns:Profile" maxOccurs="unbounded" minOccurs="1"></element>

Here we have three complex types called,




GSCProfiles is the root of all. here how is the relationship goes, 

GSCProfiles  ---< Profile ---< GSC

Once you have added the bin folder to the classpath,  go to the src folder of your project from command line and issue the following command,

xjc -d binding -p com.jaxb.gsc GSCProfiles.xsd

This will generates several classes in the above mentioned package (com.jaxb.gsc) one class for each complex type and an object factory and a package info class.

Create another class called JAXBTest  in a diffrernt package, lets name that as com.jaxb.test


Create this variable at class level. and a variable to hold the xml file name.

private static Map<String, Map<String, String>> gscProfileMap = new HashMap<String, Map<String, String>>();
* Output xml file name.
private static File xmlFile = new File("xml/GSCProfile.xml");

1. Create a method called saveGSCProfiles and add following code fragments there. this method will save the content for it.

public static void saveGSCProfiles() {
try {
Map<String, String> gscValueMap = new HashMap<String, String>();

// Adding GSC Elements

gscValueMap.put("Data Communications", "0");
gscValueMap.put("Distributed Data Processing", "0");
gscValueMap.put("Performance", "0");
gscValueMap.put("Heavily Used Configuration", "0");
gscValueMap.put("Transaction Rate", "0");
gscValueMap.put("Online Data Entry", "0");
gscValueMap.put("End User Efficiency", "0");
gscValueMap.put("Online Update", "0");
gscValueMap.put("Complex Processing", "0");
gscValueMap.put("Reusability", "0");
gscValueMap.put("Installation Ease", "0");
gscValueMap.put("Operational Ease", "0");
gscValueMap.put("Multiple Sites", "0");
gscValueMap.put("Facilitate Change", "0");
gscProfileMap.put("ProfileName", gscValueMap);
Set<String> pofileNames = gscProfileMap.keySet();

ObjectFactory factory = new ObjectFactory();

GSCProfiles gscProfiles = factory.createGSCProfiles();

for (String profileKey : pofileNames) {

Profile profile = factory.createProfile();

// Setting profile name.

gscValueMap = gscProfileMap.get(profileKey);
Set<String> gscSet = gscValueMap.keySet();

for (String gscKey : gscSet) {

GSC gsc = factory.createGSC();



JAXBContext jaxbContext = JAXBContext.newInstance("com.jaxb.gsc");

// Creating marshaller.
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

FileOutputStream outputStream = new FileOutputStream(xmlFile);

// Marshalling object in to the XML file.

marshaller.marshal(gscProfiles, outputStream);

} catch (JAXBException e) {
} catch (FileNotFoundException e) {

2. Create another method to read from the XML file. let's name this as loadGSCProfiles    

public static void loadGSCProfiles() {
try {

JAXBContext jaxbContext = JAXBContext.newInstance("com.jaxb.gsc");

// Create unmarshaller.
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

GSCProfiles gscProfiles = (GSCProfiles) unmarshaller.unmarshal(xmlFile);

// Retrieving list of profiles.
List<Profile> profile = gscProfiles.getProfile();

for (Profile profile2 : profile) {

Map<String, String> gscValueMap = new HashMap<String, String>();

List<GSC> gsc = profile2.getGSCElements();

for (GSC gsc2 : gsc) {
gscValueMap.put(gsc2.getName(), gsc2.getRating() + "");

gscProfileMap.put(profile2.getProfileName(), gscValueMap);
} catch (JAXBException e) {

Create a directory inside your Eclipse project and name it as xml, and create an xml file called  GSCProfile.xml

 Finally your Eclipse project directory structure should look like this

 Eclipse directory structure


Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.


Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


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

{{ parent.tldr }}

{{ parent.urlSource.name }}