• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Himai Minh

JAXB Marshalling, setter methods problem

Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Need your valuable inputs to generate an xml file, using jaxb marshaller.

Im able to unmarshal the xml file, able to retrieve the values from the xml file for different users. But could not generate the xml file with marshaller object. There is no setter method to add multiple users under the Users tag.

XML file contains 'n' number of 'User' entries under 'Users' tag. The 'UserID' is mandatory field for every User. I'm able to set the values for indivual User like, first name last name and status. But there was no setter method to add individual user to Users list. I'm using xjc command to generate the java objects.

Can you please look into the below sample code and help me out to generate the xml file with different User entries under Users tag.

sample xml File:

<xml version="1.0" encoding="UTF-8"?>


XSD File:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="Users" type="UsersType"/>
<xsd:complexType name="UsersType">
<xsd:element ref="User" minOccurs="0" maxOccurs="unbounded"/>

<xsd:element name="User" type="UserType"/>
<xsd:complexType name="UserType">
<xsd:element name = "UserID">
<xsd:restriction base = "xsd:string">
<xsd:maxLength value = "30"/>

<xsd:element name = "FirstName" minOccurs="0">
<xsd:restriction base = "xsd:string">

<xsd:element name = "LastName" minOccurs="0">
<xsd:restriction base = "xsd:string">
<xsd:maxLength value = "128"/>

<xsd:element name = "Status" minOccurs="0">
<xsd:restriction base = "xsd:string">
<xsd:maxLength value = "100"/>




xjc Command:
xjc -d (your_directory,C:\..) -p your_package -xmlschema xsd_name.xsd

JAVA File:

import java.io.File;
import java.io.FileOutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.ValidationException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import impl.UsersTypeImpl;

public class UserTest {

public void userTest() {
try {

ocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File("your_xml_filename.xml"));

JAXBContext jc = JAXBContext.newInstance("your package", this.getClass().getClassLoader());

// An Unmarshaller instance

Unmarshaller unm = jc.createUnmarshaller();
UsersTypeImpl users1=(UsersTypeImpl) unm.unmarshal(doc);

java.util.List usrList=users1.getUser();

System.out.println(" no.of users "+usrList.size());

int totalusers=usrList.size();

for(int i=0; i<totalusers;i++){

System.out.println("\n Loop "+i);

User usr1=(User)usrList.get(i);

String userId=usr1.getUserID();
System.out.println(" Req Id >> "+usr1.userId());

String firstName=usr1.getFirstName();


// Marshaller, Creating Java file

Marshaller m = jc.createMarshaller();

// Object Factory
ObjectFactory objFactory=new ObjectFactory();

// users object is the list of user objects
UsersType users=objFactory.createUsersType();

// User is the objject to set to Users list
User user1=(User)users.getUser();

User user2=(User)users.getUser();

// setting values for the first user
user1.setFirstName("First Name");
user1.setUserId("User Id");

User user2=objFactory.createUser();

// setting values for the second user
user2.setLastName("Last Name");
user2.setUserId("User Id");

// users
// Here Im unable to set user1 and user2 objects to list users, which is the final xml file.

// No setter method to set user1 and user2 objects to Users object
// example:
// users.setUser(user1); (Not available)
// users.setUser(user2);(Not available)
// How to set the user objects, user1 and user2 to Users object ??

m.marshal(users, System.out);
m.marshal(users,new FileOutputStream("Output.xml"));

catch(ValidationException ve){
catch (Exception error) {
public static void main(String[] args) {
UserTest t=new UserTest();
World domination requires a hollowed out volcano with good submarine access. Tiny ads are optional.
Free, earth friendly heat - from the CodeRanch trailboss
    Bookmark Topic Watch Topic
  • New Topic