File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes Marshalling with JAXB- No Parent tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Marshalling with JAXB- No Parent tag" Watch "Marshalling with JAXB- No Parent tag" New topic

Marshalling with JAXB- No Parent tag

Josweth Reddy
Ranch Hand

Joined: Nov 02, 2005
Posts: 30
Hi All,

I'm marshalling the data using JAXB.The output xml does not contain the parent tag(customer). What might be the problem.

Please find the below code.

XML File:
<?xml version="1.0"?>

XSD File:
<xsd:schema xmlns:xsd="">

<xsd:element name="customer" type="customerType"/>

<xsd:complexType name="customerType">
<xsd:element name="customer_id" type="xsd:string"/>
<xsd:element name="customer_status" type="xsd:string"/>
<xsd:element name="credit" type="creditType"/>

<xsd:complexType name="creditType">
<xsd:element name="credit_id" type="xsd:string"/>
<xsd:element name="credit_group" type="xsd:string"/>
<xsd:element name="status" type="xsd:string"/>

Java Code:
public class TestMarshal {

public TestMarshal() {
try {

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

JAXBContext jc = JAXBContext.newInstance("com.sample.jaxb", this.getClass().getClassLoader());

Marshaller mar = jc.createMarshaller();

ObjectFactory objFactory=new ObjectFactory();

CustomerType custmer=objFactory.createCustomerType();
CreditType creditType=objFactory.createCreditType();



mar.marshal( custmer, System.out );


catch(ValidationException ve){
catch (Exception error) {

public static void main(String[] args) {
new TestMarshal();

Output XML File:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

In output xml, missing out the parent tag (customer). Please help me out, so that I can have the output with parent tag(customer).

Thanks in advance,
Josweth Reddy
Josweth Reddy
Ranch Hand

Joined: Nov 02, 2005
Posts: 30
Identified the problem with java code. Modifications are as below

// Existing Code
CustomerType _cust=objFactory.createCustomerType();

// Modified the above code as
CustomerType _cust=objFactory.createCustomer();

Now able to generate the xml file with parent tag.

Josweth Reddy
I agree. Here's the link:
subject: Marshalling with JAXB- No Parent tag
It's not a secret anymore!