EasyPoXmlParser.java - Parse XML to Java Objects

Q

How to write Java program to parse XML document to 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 parse an XML document into Java data objects:

1. Use the Factory sub class generated from the "scomp" tool or the SchemaCompiler class code to parse the XML document:

      PurchaseOrderDocument poDoc = 
	     PurchaseOrderDocument.Factory.parse(new File("easypo.xml"));

2. Use get*() methods to retrieve objects representing complex elements from the XML document. Use get*Array() methods for repeating elements.

	  PurchaseOrderDocument.PurchaseOrder po = poDoc.getPurchaseOrder();
	  Customer c = po.getCustomer();
	  LineItem[] l = po.getLineItemArray();

3. Use get*() methods to retrieve primitive value or special objects representing simple elements from the XML document:

	  Customer c = po.getCustomer();
	  String n = c.getName();
	  String a = c.getAddress();

  	  Calendar d = po.getDate();

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

// Copyright (c) FYIcenter.com
import java.io.File;
import java.util.Calendar ;
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 EasyPoXmlParser {
   public static void main(String[] args) throws Exception {
   
      // Bind the incoming XML to an XMLBeans type.
      PurchaseOrderDocument poDoc = 
	     PurchaseOrderDocument.Factory.parse(new File("easypo.xml"));
		 
	  PurchaseOrderDocument.PurchaseOrder po = poDoc.getPurchaseOrder();
	  Customer c = po.getCustomer();
	  String n = c.getName();
	  String a = c.getAddress();

	  Calendar d = po.getDate();
	  LineItem[] l = po.getLineItemArray();
	  Shipper s = po.getShipper();

	  System.out.println("\r\nCustomer name: \r\n"+n);
	  System.out.println("\r\nCustomer address: \r\n"+a);
	  System.out.println("\r\nDate: \r\n"+d.toString());
	  System.out.println("\r\n1st LineItem: \r\n"+l[0].toString());
	  System.out.println("\r\nShiper: \r\n"+s.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 EasyPoXmlParser.java

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

Customer name:
Gladys Kravitz

Customer address:
Anytown, PA

Date:
2003-01-07T14:16:00-05:00

1st LineItem:
<xml-fragment xmlns:eas="http://openuri.org/easypo">
  <eas:description>Burnham's Celestial Handbook, Vol 1</eas:description>
  <eas:per-unit-ounces>5</eas:per-unit-ounces>
  <eas:price>21.79</eas:price>
  <eas:quantity>2</eas:quantity>
</xml-fragment>

Shiper:
<xml-fragment xmlns:eas="http://openuri.org/easypo">
  <eas:name>ZipShip</eas:name>
  <eas:per-ounce-rate>0.74</eas:per-ounce-rate>
</xml-fragment>

 

Using Apache XMLBeans in Java Programs

⇒⇒FAQ for Apache XMLBeans JAR Library

2017-07-07, 158👍, 0💬