mko
mko

Reputation: 7325

Serialize object to XML based on existing XSD schema

I have to create an XML document that would be based on an certain XML Schema Document. Since my data is a DataSet, I need to find the best way to start off.

I have couple of different ideas how to start:

Is this a right way to get a XML output from DataSet to match XSD schema?

Upvotes: 1

Views: 1466

Answers (1)

mle
mle

Reputation: 2550

May be you should give XMLBeans a try... It's a diverse framework for playing around with compiled XSD schemas. Compiled in this context means, you create JAVA classes from your XSD-files.

Compilation example (as can be seen here) scomp -out purchaseorder.jar purchaseorder.xsd

With this jar in your classpath you could create new a priori valid instances of your schema with something like:

public PurchaseOrderDocument createPO() {
    PurchaseOrderDocument newPODoc = PurchaseOrderDocument.Factory.newInstance();
    PurchaseOrder newPO = newPODoc.addNewPurchaseOrder();
    Customer newCustomer = newPO.addNewCustomer();
    newCustomer.setName("Doris Kravitz");
    newCustomer.setAddress("Bellflower, CA");
    return newPODoc;
}

You can find the whole example at: XMLBeans Tutorial under the heading "Creating New XML Instances from Schema".

Upvotes: 1

Related Questions