Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Marshalling with JAXB- No Parent tag

 
Josweth Reddy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic