aspose file tools*
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
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Marshalling with JAXB- No Parent tag" Watch "Marshalling with JAXB- No Parent tag" New topic
Author

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"?>
<customer>
<customer_id>custId</customer_id>
<customer_status>custStatus</customer_status>
<credit>
<credit_id>creditid</credit_id>
<credit_group>creditgroup</credit_group>
<status>creditstatus</status>
</credit>
</customer>

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

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

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

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


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
Marshaller mar = jc.createMarshaller();

ObjectFactory objFactory=new ObjectFactory();

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

custmer.setCustomerId("CustomerId");
custmer.setCustomerStatus("New");
custmer.setCredit(creditType);


creditType.setCreditGroup("CreditGroup");
creditType.setCreditId("CreditId");
creditType.setStatus("Updated");

mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE);
mar.marshal( custmer, System.out );

}

catch(ValidationException ve){
ve.printStackTrace();
}
catch (Exception error) {
error.printStackTrace();
}
}

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

Output XML File:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer_id>CustomerId</customer_id>
<customer_status>New</customer_status>
<credit>
<credit_id>CreditGroup</credit_id>
<credit_group>CreditId</credit_group>
<status>Updated</status>
</credit>

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.

Thanks,
Josweth Reddy
 
 
subject: Marshalling with JAXB- No Parent tag
 
Similar Threads
XML not getting validated with schema... + strange...
XML generation pbm with JAXB Marshaler
Help in creating XML Schema
Top_Level Definition
JAXB Marshalling, setter methods problem