Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (101)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (309)
Collections:
Other Resources:
EasyPoXmlGenerator.java - Generate XML from Java Objects
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
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>
⇒ FAQ for Apache XMLBeans JAR Library
⇐ EasyPoXmlParser.java - Parse XML to Java Objects
2017-07-07, 1834🔥, 0💬
Popular Posts:
JDK 11 jdk.scripting.nashorn.jm odis the JMOD file for JDK 11 Scripting Nashorn module. JDK 11 Scrip...
The JDT project provides the tool plug-ins that implement a Java IDE supporting the development of a...
xml-commons Resolver Source Code Files are provided in the source package file, xml-commons-resolver...
What Is poi-5.2.3.jar? poi-5.2.3.jar is one of the JAR files for Apache POI 5.2.3, which provides an...
How to download and install javamail-1_2.zip? The JavaMail API is a set of abstract APIs that model ...