EasyPoXmlGenerator.java - Generate XML from Java Objects

Q

How to write Java program to generate XML document from Java data type objects using XMLBeans? I have the XML Schema compiled as Java data types.

✍: FYIcenter.com

A

If you have your XML schema compiled as Java data types, you can following these suggestions to write a Java program to generate an XML document from Java data objects:

1. Use the Factory sub class generated from the "scomp" tool or the SchemaCompiler class code to create a new empty XML document object:

      PurchaseOrderDocument poDoc = PurchaseOrderDocument.Factory.newInstance();
	  PurchaseOrderDocument.PurchaseOrder po 
	     = PurchaseOrderDocument.PurchaseOrder.Factory.newInstance();

2. Use Factory sub classes of complex element types to create new empty XML element objects:

      Customer c = Customer.Factory.newInstance();
      Shipper s = Shipper.Factory.newInstance();

2. For repeating elements, use the insertNew*() methods of the parent object to create new empty XML element objects:

	  LineItem l = po.insertNewLineItem(0);
 	  LineItem m = po.insertNewLineItem(1);

3. Use set*() methods to assign new primitive values to simple elements and attributes:

	  c.setName("Frank Y. Ivy");
	  c.setAddress("Anywhere, EU");
	  c.setAge(18);

4. Some simple element types require special Java data type classes:

      l.setPerUnitOunces(new BigDecimal(9.0));
      l.setPrice(new BigDecimal(499.99));
      l.setQuantity(new BigInteger("1"));

5. Use set*() methods assign sub elements to parent elements. Make sure to complete sub elements before assigning them to the parent.

      po.setCustomer(c); 
      po.setDate(d);
      po.setShipper(s);
	  poDoc.setPurchaseOrder(po);

6. Use toString() method to generate the XML document from the root element object:

	  System.out.println("\r\neEasyPO xml: \r\n"+poDoc.toString());

Below is the entire sample Java program that generate an XML document into objects of data type classes generated by XMLBeans:

// Copyright (c) FYIcenter.com
import java.util.Calendar ;
import java.math.*;
import org.apache.xmlbeans.*;
import org.openuri.easypo.PurchaseOrderDocument;
import org.openuri.easypo.Customer;
import org.openuri.easypo.LineItem;
import org.openuri.easypo.Shipper;

public class EasyPoXmlGenerator {
   public static void main(String[] args) throws Exception {
      // Create an empty instance of an XMLBeans type.
      PurchaseOrderDocument poDoc = PurchaseOrderDocument.Factory.newInstance();

	  PurchaseOrderDocument.PurchaseOrder po 
	     = PurchaseOrderDocument.PurchaseOrder.Factory.newInstance();
	  
      Customer c = Customer.Factory.newInstance();
	  c.setName("Frank Y. Ivy");
	  c.setAddress("Anywhere, EU");
	  c.setAge(18);
      po.setCustomer(c); 
	  
	  Calendar d = Calendar.getInstance();
	  d.set(1970,1,1);
      po.setDate(d);

	  LineItem l = po.insertNewLineItem(0);
	  l.setDescription("Google Phone");
      l.setPerUnitOunces(new BigDecimal(9.0));
      l.setPrice(new BigDecimal(499.99));
      l.setQuantity(new BigInteger("1"));
 
 	  LineItem m = po.insertNewLineItem(1);
	  m.setDescription("Phone Battery");
      m.setPerUnitOunces(new BigDecimal(3.0));
      m.setPrice(new BigDecimal(9.99));
      m.setQuantity(new BigInteger("1"));
 
      Shipper s = Shipper.Factory.newInstance();
      s.setName("FastShipper");
	  s.setPerOunceRate(new BigDecimal(0.99));
      po.setShipper(s);
	  
	  poDoc.setPurchaseOrder(po);
	  System.out.println("\r\nEasyPO xml: \r\n"+poDoc.toString());
   }
}

Compile and run it with Java SE 8 JDK:

\fyicenter\xmlbeans-2.6.0>\fyicenter\jdk-1.8.0\bin\javac 
   -cp .\lib\xbean.jar;easypo.jar EasyPoXmlGenerator.java

\fyicenter\xmlbeans-2.6.0>\fyicenter\jdk-1.8.0\bin\java 
   -cp .;.\lib\xbean.jar;easypo.jar EasyPoXmlGenerator

EasyPO xml:
<purchase-order xmlns="http://openuri.org/easypo">
  <customer age="18">
    <name>Frank Y. Ivy</name>
    <address>Anywhere, EU</address>
  </customer>
  <date>1970-02-01T19:42:53.812-05:00</date>
  <line-item>
    <description>Google Phone</description>
    <per-unit-ounces>9</per-unit-ounces>
    <price>499.990000000000009094947017729282379150390625</price>
    <quantity>1</quantity>
  </line-item>
  <line-item>
    <description>Phone Battery</description>
    <per-unit-ounces>3</per-unit-ounces>
    <price>9.9900000000000002131628207280300557613372802734375</price>
    <quantity>1</quantity>
  </line-item>
  <shipper>
    <name>FastShipper</name>
    <per-ounce-rate>0.9899999999999999911182158029987476766109466552734375</per-ounce-rate>
  </shipper>
</purchase-order>

 

Using Apache XMLBeans in Java Programs

⇒⇒FAQ for Apache XMLBeans JAR Library

2017-07-07, 382👍, 0💬