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:
EasyPoXmlParser.java - Parse XML to Java Objects
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
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>
⇒ EasyPoXmlGenerator.java - Generate XML from Java Objects
⇐ easypo.xml - Purchase Order Test XML Document
2017-07-07, 1587🔥, 0💬
Popular Posts:
pache Derby is an open source relational database implemented entirely in Java and available under t...
How to download and install ojdbc14.jar for Oracle 10g R2? ojdbc14.jar for Oracle 10g R2 is a Java 1...
Apache Axis2 is the core engine for Web services. It is a complete re-design and re-write of the wid...
SLF4J API is a simple API that allows to plug in any desired logging library at deployment time. Her...
Commons VFS provides a single API for accessing various different file systems. It presents a unifor...