Categories:
Audio (13)
Biotech (29)
Bytecode (35)
Database (77)
Framework (7)
Game (7)
General (512)
Graphics (53)
I/O (32)
IDE (2)
JAR Tools (86)
JavaBeans (16)
JDBC (89)
JDK (337)
JSP (20)
Logging (103)
Mail (54)
Messaging (8)
Network (71)
PDF (94)
Report (7)
Scripting (83)
Security (32)
Server (119)
Servlet (17)
SOAP (24)
Testing (50)
Web (19)
XML (301)
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>
Â
2017-07-07, 1680👍, 0💬
Popular Posts:
Xalan-Java, Version 2.7.1, is an XSLT processor for transforming XML documents into HTML, text, or o...
Apache Log4j 1.2 Bridge allows applications coded to use Log4j 1.2 API to use Log4j 2 instead. Bytec...
MXP1 is a stable XmlPull parsing engine that is based on ideas from XPP and in particular XPP2 but c...
What Is mail.jar of JavaMail 1.4.2? I got the JAR file from javamail-1.4.2.zip. mail.jar in javamail...
Jaxen, Release 1.1.1, is an open source XPath library written in Java. It is adaptable to many diffe...