This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Web Services and the fly likes How to generate an xml file from xsd Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "How to generate an xml file from xsd" Watch "How to generate an xml file from xsd" New topic
Author

How to generate an xml file from xsd

Nonty Mbowane
Greenhorn

Joined: Jan 23, 2013
Posts: 2
Hi all,

I'm new to this JAXBElement can you please assist me, i saw and example of Using JAXB to generate XML from XSD, i did the example i found here it works perferct, when i implement it using my xsd it only print out 2 lines of code for xml.

my xsd looks like this

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Instruction" targetNamespace="http://interactive.advantage.am/instructionUpload" elementFormDefault="qualified" xmlns="http://interactive.advantage.am/instructionUpload" xmlns:mstns="http://tempuri.org/Instruction.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="InstructionUpload">
<xs:complexType>
<xs:sequence>
<xs:element name="AdministratorCode" type="xs:string" />
<xs:element name="PreparedBy" type="xs:string" />
<xs:element name="PrepareDate" type="xs:dateTime" />
<xs:element name="SubmitDate" type="xs:dateTime" />
<xs:element name="ResponseDate" type="xs:dateTime" nillable="true" />
<xs:element name="ResponseType" type="ResponseType" nillable="true" />

<xs:element name="Instructions" type="Instructions" minOccurs="1" maxOccurs="1" />
<xs:element name="InstructionControlCount" type="xs:integer" />
<xs:element name="InstructionTransactionControlCount" type="xs:integer" />
<xs:element name="InstructionControlTotal" type="xs:decimal" />
<xs:element name="InstructionTransactionControlTotal" type="xs:decimal" />

</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="Instructions">
<xs:sequence>
<xs:element name="Instruction" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="UniqueIdentifier" type="xs:string" />

<xs:element name="FundCode" type="xs:string" />

<xs:element name="InstructionTypeCode" type="InstructionTypeCode" />

<xs:element name="ReferenceTypeCode" type="ReferenceTypeCode" />

<xs:element name="InstructionModeCode" type="InstructionModeCode" />

<xs:element name="Value" type="valueDecimal" />
<xs:element name="Date" type="xs:dateTime" />

<xs:element name="AdministratorReference" type="xs:string" />

<xs:element name="Comments" type="xs:string" />
<xs:element name="IsConfirmed" type="xs:boolean" minOccurs="0" nillable="1" />

<xs:element name="InstructionTransactions" type="InstructionTransactions" minOccurs="1" maxOccurs="1" />
<xs:element name="Errors" type="Errors" minOccurs="0" maxOccurs="1" />
</xs:sequence>
<xs:attribute name="id" type="xs:integer" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="InstructionTransactions">
<xs:sequence>
<xs:element name="InstructionTransaction" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="UniqueIdentifier" type="xs:string" />

<xs:element name="PortfolioTypeCode" type="PortfolioTypeCode" />

<xs:element name="PortfolioCode" type="xs:string" />

<xs:element name="Value" type="valueDecimal" />
<xs:element name="SplitPercentage" type="percentageDecimal" />
<xs:element name="FinalUnitPrice" type="valueDecimal" nillable="1" />

<xs:element name="FinalUnits" type="valueDecimal" nillable="1" />

<xs:element name="FinalValue" type="valueDecimal" nillable="1" />

<xs:element name="ExecuteDate" type="xs:dateTime" nillable="1" />

<xs:element name="IsConfirmed" type="xs:boolean" minOccurs="0" nillable="1" />

</xs:sequence>
<xs:attribute name="id" type="xs:integer" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Errors">
<xs:sequence>
<xs:element name="Error" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Description" type="xs:string" />
</xs:sequence>
<xs:attribute name="code" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="InstructionTypeCode">
<xs:restriction base="xs:string">
<xs:enumeration value="C" id="C" />
<xs:enumeration value="WDW" id="WDW" />
<xs:enumeration value="SWI" id="SWI" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="ReferenceTypeCode">
<xs:restriction base="xs:string">
<xs:enumeration value="DEFAULT" id="DEFAULT" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="InstructionModeCode">
<xs:restriction base="xs:string">
<xs:enumeration value="AMOUNT" id="AMOUNT" />
<xs:enumeration value="UNITS" id="UNIT" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="percentageDecimal">
<xs:restriction base="xs:decimal">
<xs:fractionDigits value="12" />
<xs:maxInclusive value="1" />
<xs:minInclusive value="0" />
<xs:totalDigits value="14" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="valueDecimal">
<xs:restriction base="xs:decimal">
<xs:fractionDigits value="12" />
<xs:totalDigits value="24" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="PortfolioTypeCode">
<xs:restriction base="xs:string">
<xs:enumeration value="S" />
<xs:enumeration value="T" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="ResponseType">
<xs:restriction base="xs:string">
<xs:enumeration value="F" />
</xs:restriction>
</xs:simpleType>

</xs:schema>

then i generated java classes from the xsd, in my main class
import java.math.BigDecimal;
import java.math.BigInteger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;


import am.advantage.interactive.instructionupload.InstructionModeCode;
import am.advantage.interactive.instructionupload.InstructionTransactions;
import am.advantage.interactive.instructionupload.InstructionTransactions.InstructionTransaction;
import am.advantage.interactive.instructionupload.InstructionTypeCode;
import am.advantage.interactive.instructionupload.InstructionUpload;
import am.advantage.interactive.instructionupload.Instructions;
import am.advantage.interactive.instructionupload.PortfolioTypeCode;
import am.advantage.interactive.instructionupload.Instructions.Instruction;
import am.advantage.interactive.instructionupload.ObjectFactory;
import am.advantage.interactive.instructionupload.ReferenceTypeCode;


public class Main {


public static void main(String[]args) throws JAXBException, DatatypeConfigurationException{

ObjectFactory factory = new ObjectFactory();

InstructionTransaction instructionTransactionItem = factory.createInstructionTransactionsInstructionTransaction();
instructionTransactionItem.setUniqueIdentifier("");
instructionTransactionItem.setPortfolioTypeCode(PortfolioTypeCode.T);
instructionTransactionItem.setPortfolioCode("");
instructionTransactionItem.setValue(null);
instructionTransactionItem.setSplitPercentage(null);
instructionTransactionItem.setFinalUnitPrice(null);
instructionTransactionItem.setFinalUnits(null);
instructionTransactionItem.setFinalValue(null);
XMLGregorianCalendar date = DatatypeFactory.newInstance().newXMLGregorianCalendar();
instructionTransactionItem.setExecuteDate(date);
JAXBElement<Boolean> jaxtrue = new JAXBElement<Boolean>(QName.valueOf(""), Boolean.TYPE, Boolean.TRUE);
instructionTransactionItem.setIsConfirmed(jaxtrue);

InstructionTransactions instructionTransactionsItem = factory.createInstructionTransactions();
instructionTransactionsItem.getInstructionTransaction().add(instructionTransactionItem);

Instruction instructionItem = factory.createInstructionsInstruction();
instructionItem.setUniqueIdentifier("");
instructionItem.setFundCode("");
instructionItem.setInstructionTypeCode(InstructionTypeCode.C);
instructionItem.setReferenceTypeCode(ReferenceTypeCode.DEFAULT);
instructionItem.setInstructionModeCode(InstructionModeCode.AMOUNT);
instructionItem.setValue(null);
XMLGregorianCalendar insdate = DatatypeFactory.newInstance().newXMLGregorianCalendar();
instructionItem.setDate(insdate);
instructionItem.setAdministratorReference("AdministratorReference");
instructionItem.setComments("");
JAXBElement<Boolean> jaxbtrue = new JAXBElement<Boolean>(QName.valueOf(""), Boolean.TYPE, Boolean.TRUE);
instructionItem.setIsConfirmed(jaxbtrue);
instructionItem.setInstructionTransactions(instructionTransactionsItem);

Instructions instructionsItem = factory.createInstructions();
instructionsItem.getInstruction().add(instructionItem);

InstructionUpload instructionUploadItem = factory.createInstructionUpload();
instructionUploadItem.setAdministratorCode("");
instructionUploadItem.setPreparedBy("");
instructionUploadItem.setPrepareDate(date);
instructionUploadItem.setSubmitDate(date);
instructionUploadItem.setResponseDate(null);
instructionUploadItem.setResponseType(null);
instructionUploadItem.setInstructionControlCount(new BigInteger(""));
instructionUploadItem.setInstructionTransactionControlCount(new BigInteger(""));
instructionUploadItem.setInstructionControlTotal(new BigDecimal(""));
instructionUploadItem.setInstructionTransactionControlTotal(new BigDecimal(""));

InstructionUpload instructionUpload = factory.createInstructionUpload();
instructionUploadItem.setInstructions(instructionsItem);

JAXBContext context = JAXBContext.newInstance("am.advantage.interactive.instructionupload");
JAXBElement<InstructionUpload> element = factory.createInstructionUploadReport(instructionUploadItem);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty("jaxb.formatted.output", Boolean.TRUE);
marshaller.marshal(element, System.out);
}

}

when i run this class it prints

Exception in thread "main" java.lang.NullPointerException
at am.advantage.interactive.instructionupload.test.Main.main(Main.java:71)



when i comment out the instructionUpload

package am.advantage.interactive.instructionupload.test;


import java.math.BigDecimal;
import java.math.BigInteger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;


import am.advantage.interactive.instructionupload.InstructionModeCode;
import am.advantage.interactive.instructionupload.InstructionTransactions;
import am.advantage.interactive.instructionupload.InstructionTransactions.InstructionTransaction;
import am.advantage.interactive.instructionupload.InstructionTypeCode;
import am.advantage.interactive.instructionupload.InstructionUpload;
import am.advantage.interactive.instructionupload.Instructions;
import am.advantage.interactive.instructionupload.PortfolioTypeCode;
import am.advantage.interactive.instructionupload.Instructions.Instruction;
import am.advantage.interactive.instructionupload.ObjectFactory;
import am.advantage.interactive.instructionupload.ReferenceTypeCode;


public class Main {


public static void main(String[]args) throws JAXBException, DatatypeConfigurationException{

ObjectFactory factory = new ObjectFactory();

InstructionTransaction instructionTransactionItem = factory.createInstructionTransactionsInstructionTransaction();
instructionTransactionItem.setUniqueIdentifier("");
instructionTransactionItem.setPortfolioTypeCode(PortfolioTypeCode.T);
instructionTransactionItem.setPortfolioCode("");
instructionTransactionItem.setValue(null);
instructionTransactionItem.setSplitPercentage(null);
instructionTransactionItem.setFinalUnitPrice(null);
instructionTransactionItem.setFinalUnits(null);
instructionTransactionItem.setFinalValue(null);
XMLGregorianCalendar date = DatatypeFactory.newInstance().newXMLGregorianCalendar();
instructionTransactionItem.setExecuteDate(date);
JAXBElement<Boolean> jaxtrue = new JAXBElement<Boolean>(QName.valueOf(""), Boolean.TYPE, Boolean.TRUE);
instructionTransactionItem.setIsConfirmed(jaxtrue);

InstructionTransactions instructionTransactionsItem = factory.createInstructionTransactions();
instructionTransactionsItem.getInstructionTransaction().add(instructionTransactionItem);

Instruction instructionItem = factory.createInstructionsInstruction();
instructionItem.setUniqueIdentifier("");
instructionItem.setFundCode("");
instructionItem.setInstructionTypeCode(InstructionTypeCode.C);
instructionItem.setReferenceTypeCode(ReferenceTypeCode.DEFAULT);
instructionItem.setInstructionModeCode(InstructionModeCode.AMOUNT);
instructionItem.setValue(null);
XMLGregorianCalendar insdate = DatatypeFactory.newInstance().newXMLGregorianCalendar();
instructionItem.setDate(insdate);
instructionItem.setAdministratorReference("AdministratorReference");
instructionItem.setComments("");
JAXBElement<Boolean> jaxbtrue = new JAXBElement<Boolean>(QName.valueOf(""), Boolean.TYPE, Boolean.TRUE);
instructionItem.setIsConfirmed(jaxbtrue);
instructionItem.setInstructionTransactions(instructionTransactionsItem);

Instructions instructionsItem = factory.createInstructions();
instructionsItem.getInstruction().add(instructionItem);

InstructionUpload instructionUploadItem = factory.createInstructionUpload();
// instructionUploadItem.setAdministratorCode("");
// instructionUploadItem.setPreparedBy("");
// instructionUploadItem.setPrepareDate(date);
// instructionUploadItem.setSubmitDate(date);
// instructionUploadItem.setResponseDate(null);
// instructionUploadItem.setResponseType(null);
// instructionUploadItem.setInstructionControlCount(new BigInteger(""));
// instructionUploadItem.setInstructionTransactionControlCount(new BigInteger(""));
// instructionUploadItem.setInstructionControlTotal(new BigDecimal(""));
// instructionUploadItem.setInstructionTransactionControlTotal(new BigDecimal(""));

//
InstructionUpload instructionUpload = factory.createInstructionUpload();
instructionUploadItem.setInstructions(instructionsItem);

JAXBContext context = JAXBContext.newInstance("am.advantage.interactive.instructionupload");
JAXBElement<InstructionUpload> element = factory.createInstructionUploadReport(instructionUploadItem);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty("jaxb.formatted.output", Boolean.TRUE);
marshaller.marshal(element, System.out);
}

}

it prints

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<InstructionUpload xmlns:ns2="http://interactive.advantage.am/instructionUpload" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>


your help will be much appreciated
Thanks.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 499
    
    3
But before doing marshalling testing, did you not find error(s) when you compiled the schema? (I think you probably should find some errors if you look into it.) You should have them debugged before doing anything else.
Nonty Mbowane
Greenhorn

Joined: Jan 23, 2013
Posts: 2
Thanks g tsuji, I noticed that the grammar of xsd had error, I recopied it and generated java classes and when I check my code I was suppose to write it using the bottom up approach of which I didn't use in the code, but anyhow I managed to generate an xml using marshaller.
 
Don't get me started about those stupid light bulbs.
 
subject: How to generate an xml file from xsd
 
Similar Threads
XSD element validation
Axis 1.4 throwing org.xml.sax.SAXException: Invalid element
MS Exchange web service: If the 'nillable' attribute is false in the schema, the 'xsi:nil' attribute
javax.xml.bind .UnmarshalException in JAXB
unble to validate xml against schema