• 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 ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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"?>
<Users>
<User>
<UserID/>
<FirstName/>
<LastName/>
<Status/>
</User>

<User>
<UserID/>
<FirstName/>
<LastName/>
<Status/>
</User>
</Users>


XSD File:
----------

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

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

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


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


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


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

</xsd:sequence>

</xsd:complexType>

</xsd:schema>



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.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE);
m.marshal(users, System.out);
m.marshal(users,new FileOutputStream("Output.xml"));

}
catch(ValidationException ve){
ve.printStackTrace();
}
catch (Exception error) {
error.printStackTrace();
}
}
public static void main(String[] args) {
UserTest t=new UserTest();
t.userTest();
}
}
 
I just had the craziest dream. This tiny ad was in it.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic