Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (5.26 MB, 774 trang )
Examples Using XML Java Class Generator with DTDs and XML Schema
PurchaseOrder.PurchaseOrder_Type.ShipTo shipTo =
new PurchaseOrder.PurchaseOrder_Type.ShipTo();
// Create Address
Address address = new Address();
// Create the Name for the address and add
// it to addresss
Address.Name name = new Address.Name();
name.setType("Mary Smith");
address.addName(name);
// Create the Stree name for the address and add
// it to the address
Address.Street street = new Address.Street();
street.setType("Laurie Meadows");
address.addStreet(street);
// Create the city name for the address and add
// it to the address
Address.City city = new Address.City();
city.setType("San Mateo");
address.addCity(city);
// Create the zip name for the address and add
// it to the address
Address.Zip zip = new Address.Zip();
zip.setType(new Double("11208"));
address.addZip(zip);
// Set the address of the shipTo object
shipTo.setType(address);
// Add the shipTo to the Purchase Type object
poType.addShipTo(shipTo);
// Create a Purchase Order BillTo item
PurchaseOrder.PurchaseOrder_Type.BillTo billTo =
new PurchaseOrder.PurchaseOrder_Type.BillTo();
// Create a billing Address
Address billingAddress = new Address();
// Create the name for billing address, set the
// name and add it to the billing address
Address.Name name1 = new Address.Name();
XML Class Generator for Java 7-27
Examples Using XML Java Class Generator with DTDs and XML Schema
name1.setType("John Smith");
billingAddress.addName(name1);
// Create the street name for the billing address,
// set the street name value and add it to the
// billing address
Address.Street street1 = new Address.Street();
street1.setType("No 1. North Broadway");
billingAddress.addStreet(street1);
// Create the City name for the address, set the
// city name value and add it to the billing address
Address.City city1 = new Address.City();
city1.setType("New York");
billingAddress.addCity(city1);
// Create the Zip for the address, set the zip
// value and add it to the billing address.
Address.Zip zip1 = new Address.Zip();
zip1.setType(new Double("10006"));
billingAddress.addZip(zip1);
// Set the type of the billTo object to billingAddress
billTo.setType(billingAddress);
// Add the billing address to the PurchaseOrder type
poType.addBillTo(billTo);
PurchaseOrder.PurchaseOrder_Type.Items pItem =
new PurchaseOrder.PurchaseOrder_Type.Items();
Items items = new Items();
Items.Item item = new Items.Item();
Items.Item.Item_Type itemType = new Items.Item.Item_Type();
Items.Item.Item_Type.ProductName pname =
new Items.Item.Item_Type.ProductName();
pname.setType("Perfume");
itemType.addProductName(pname);
Items.Item.Item_Type.Quantity qty =
new Items.Item.Item_Type.Quantity();
qty.setType(new Integer("1"));
itemType.addQuantity(qty);
7-28
Oracle9i XML Developer’s Kits Guide - XDK
Frequently Asked Questions About the Class Generator for Java
Items.Item.Item_Type.Price price =
new Items.Item.Item_Type.Price();
price.setType(new Double("69.99"));
itemType.addPrice(price);
Items.Item.Item_Type.ShipDate sdate =
new Items.Item.Item_Type.ShipDate();
sdate.setType("Feb 14. 2000");
itemType.addShipDate(sdate);
itemType.setPartNum("ITMZ411");
item.setType(itemType);
items.addItem(item);
pItem.setType(items);
poType.addItems(pItem);
// Set the type of the Purchase Order object to
// Purchase Order Type
po.setType(poType);
po.print(out);
out.writeNewLine();
out.flush();
}
catch (InvalidContentException e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
catch (Exception e)
{
System.out.println(e.toString());
e.printStackTrace();
}
}
}
Frequently Asked Questions About the Class Generator for Java
This section lists XML Java Class Generator questions and answers.
XML Class Generator for Java 7-29
Frequently Asked Questions About the Class Generator for Java
How Do I Install the XML Class Generator for Java?
Answer: The Class Generator is packaged as part of the XDK and so you do not
have to download it separately. The CLASSPATH should be set to include
classgen.jar, xmlparserv2.jar, and xschema.jar which are located in the
lib/ directory and not in the bin/ directory.
What Does the XML Class Generator for Java Do?
What does the XML Class Generator for Java do? How do I use it to get XML data?
Answer: The XML Class Generator for Java creates Java source files from an XML
DTD. This is useful when you need an application to send an XML message to
another application based on an agreed-upon DTD or as the back end of a Web form
to construct an XML document. Using these classes, Java applications can construct,
validate, and print XML documents that comply with the input DTD. The Class
Generator works in conjunction with the Oracle XML Parser for Java version 2,
which parses the DTD and passes the parsed document to the class generator.
To get XML data, first, get the data from the database using JDBC ResultSets. Then,
instantiate objects using the classes generated by the XML Class Generator.
Which DTDs Are Supported?
Does XML Java Class Generator support any kind of DTD?
Answer: Yes, it supports any kind of DTD that is XML 1.0 compliant.
Why Do I Get a "Classes Not Found" Error?
Why do I get a "Class Not Found" error when running XML Class Generator
samples?
Answer: Correct your CLASSPATH to include classgen.jar, xmlparserv2.jar,
and xschema.jar.
In XML Class Generator, How Do I Create the Root Object More Than Once?
I generated, from a DTD, a set of Java classes with the Class Generator. Since then,
I’ve tried to create a Java application that uses these classes to create an XML file
from data passed as arguments. I cannot create the root object, the object derived
from CGDocument, more than one time because I obtain the following error
message:
7-30
Oracle9i XML Developer’s Kits Guide - XDK
Frequently Asked Questions About the Class Generator for Java
oracle.xml.parser.XMLDOMException: Node doesn’t belong to the current document
How do I handle the star operator (*)? When the application starts I do not know
how many times the element will appear. Thus I do not build a static loop where I
make a sequence of element.addNode(). The problem is that some of these will
be empty and I will obtain an XML document with a set of empty elements with
empty attributes.
Answer: You can create subsequent XML docs by calling the constructor each time.
A well-formed XML document is not permitted to have more than one root node,
therefore you cannot use the star operator (*) on the element you are designating as
the document root.
How Can I Create XML Files from Scratch Using the DOM API?
I want to create an XML file using the DOM API. I do not want to create the XML
file by typing in the text editor:
Instead, I want to create it using the DOM API. There are several examples of
manipulating an XML file using the DOM once there is an input file, but not the
other way round. That is, of creating the XML file from scratch (when there is no
input file) using the DOM, once you know the tagnames and their values.
Answer: The simplest way is download XML Class Generator for Java and give it a
DTD of the XML document you want. It will create the DOM classes to
programmatically create XML documents. There are samples included with the
software.
Can I Create an XML Document in a Java Class?
I need to create an XML document in a Java class as follows
XML Class Generator for Java 7-31
Frequently Asked Questions About the Class Generator for Java
Can I use the XMLDocument class to create an XML document? I know about the
XML SQL Utility, but that only creates XML based on SQL queries, which is not
what I am after on this occasion. Do you have an example of how to do this?
Answer: The XML Class Generator, available as part of the Oracle XDK for Java,
does the job nicely. The XDKs are also available with Oracle9i and Oracle9i
Application Server products. The Class Generator generates Java classes for each
element in your DTD. These classes can then be used to dynamically construct an
XML document at runtime. The Class Generator download contains sample code.
7-32
Oracle9i XML Developer’s Kits Guide - XDK
8
XML SQL Utility (XSU)
This chapter contains the following sections:
s
What Is XML SQL Utility (XSU)?
s
XSU Dependencies and Installation
s
XML SQL Utility and the Bigger Picture
s
SQL-to-XML and XML-to-SQL Mapping Primer
s
How XML SQL Utility Works
s
Using the XSU Command Line Front End, OracleXML
s
XSU Java API
s
Generating XML with XSU’s OracleXMLQuery
s
Paginating Results: skipRows and maxRows
s
Generating XML from ResultSet Objects
s
Raising No Rows Exception
s
Storing XML Back in the Database Using XSU OracleXMLSave
s
Insert Processing Using XSU (Java API)
s
Update Processing Using XSU (Java API)
s
Delete Processing Using XSU (Java API)
s
Advanced XSU Usage Techniques
s
Frequently Asked Questions About XML SQL Utility (XSU)
XML SQL Utility (XSU) 8-1
What Is XML SQL Utility (XSU)?
What Is XML SQL Utility (XSU)?
XML has become the format for data interchange. At the same time, a substantial
amount of business data resides in object-relational databases. It is therefore
necessary to have the ability to transform this relational data to XML.
XML SQL Utility (XSU) enables you to do this as follows:
s
s
s
XSU can transform data retrieved from object-relational database tables or
views into XML.
XSU can extract data from an XML document, and using a canonical mapping,
insert the data into appropriate columns or attributes of a table or a view.
XSU can extract data from an XML document and apply this data to updating
or deleting values of the appropriate columns or attributes.
Generating XML from the Database
For example, on the XML generation side, when given the query SELECT * FROM
emp, XSU queries the database and returns the results as the following XML
document:
Storing XML in the Database
Going the other way, given the XML document preceding, XSU can extract the data
from it and insert it into the scott.emp table in the database.
Accessing XSU Functionality
XML SQL Utility functionality can be accessed in the following ways:
8-2
Oracle9i XML Developer’s Kits Guide - XDK
What Is XML SQL Utility (XSU)?
s
Through a Java API
s
Through a PL/SQL API
s
Through a Java command line front end
See Also:
s
Oracle9i XML API Reference - XDK and Oracle XML DB
XSU Features
XSU can perform the following tasks:
s
s
s
s
s
s
Generate XML documents from any SQL query. XSU virtually supports all the
datatypes supported in the Oracle9i database server.
Dynamically generate DTDs (Document Type Definitions).
During generation, perform simple transformations, such as modifying default
tag names for the ROW element. You can also register an XSL transformation
which is then applied to the generated XML documents as needed.
Generate XML documents in their string or DOM representations.
Insert XML into database tables or views. XSU can also update or delete records
records from a database object, given an XML document.
Easily generate complex nested XML documents. XSU can also store them in
relational tables by creating object views over the flat tables and querying over
these views. Object views can create structured data from existing relational
data using Oracle8i and Oracle9i’s object-relational infrastructure.
XSU Oracle9i New Features
Starting in Oracle9i, XSU can also perform the following tasks:
s
Generates XML Schema given an SQL Query.
s
Generates XML as a stream of SAX2 callbacks.
s
s
Supports XML attributes during generation. This provides an easy way to
specify that a particular column or group of columns should be mapped to an
XML attribute instead of an XML element.
SQL identifier to XML identifier escaping. Sometimes column names are not
valid XML tag names. To avoid this you can either alias all the column names or
turn on tag escaping.
XML SQL Utility (XSU) 8-3
XSU Dependencies and Installation
Note: Oracle9i introduced the DBMS_XMLGen PL/SQL supplied
package. This package provides the functionality previously
available with DBMS_XMLQuery. DBMS_XMLGen is built into the
database code, hence, it provides better performance.
XSU Supports XMLType
From Oracle9i Release 2 (9.2), XSU supports XMLType. Using XSU with XMLType is
useful if, for example, you have XMLType columns in objects or tables.
See Also: Oracle9i XML Database Developer’s Guide - Oracle XML
DB, in particular, the chapter on Generating XML, for examples on using
XSU with XMLType.
XSU Dependencies and Installation
Dependencies
XML SQL Utility (XSU) needs the following components:
s
s
Database connectivity -- JDBC drivers. XSU can work with any JDBC driver
but is optimized for Oracle JDBC drivers. Oracle does not make any guarantee
or provide support for the XSU running against non-Oracle databases.
XML Parser -- Oracle XML Parser, Version2. Oracle XML Parser, version 2 is
included in Oracle8i and Oracle9i, and is also available as part of the XSU install
(XDK for Java) downloadable from the Oracle Technology Network (OTN) Web
site.
Installation
XML SQL Utility (XSU) is packaged with Oracle8i (8.1.7 and later) and Oracle9i.
XSU is made up of three files:
s
s
8-4
$ORACLE_HOME/rdbms/jlib/xsu12.jar -- Contains all the Java classes
which make up XSU. xsu12 requires JDK1.2.x and JDBC2.x. This is the
XSU version loaded into Oracle9i.
$ORACLE_HOME/rdbms/jlib/xsu111.jar -- Contains the same classes as
xsu12.jar, except that xsu111 requires JDK1.1.x and JDBC1.x.
Oracle9i XML Developer’s Kits Guide - XDK
XML SQL Utility and the Bigger Picture
s
$ORACLE_HOME/rdbms/admin/dbmsxsu.sql -- This is the SQL script that
builds the XSU PL/SQL API. xsu12.jar needs to be loaded into the database
before dbmsxsu.sql is executed.
By default the Oracle9i installer installs XSU on the hard drive in the locations
specified earlier. It also loads it into the database.
If during initial installation you choose to not install XSU, you can install it later, but
the installation becomes less simple. To install XSU later, first install XSU and its
dependent components on your system. You can accomplish this using Oracle
Installer. Next perform the following steps:
1.
If you have not yet loaded XML Parser for Java in the database, go to
$ORACLE_HOME/xdk/lib. Here you will find xmlparserv2.jar that you
need to load into the database. To do this, see “Loading JAVA Classes” in the
Oracle9i Java Stored Procedures Developer’s Guide
2.
Go to $ORACLE_HOME/admin and run catxsu.sql
Note: XML SQL Utility (XSU) is part of the XDK for Java and is
also available on OTN at: http://otn.oracle.com/tech/xml
XML SQL Utility and the Bigger Picture
XML SQL Utility (XSU) is written in Java, and can live in any tier that supports
Java.
XML SQL Utility in the Database
The Java classes which make up XSU can be loaded into Java-enabled Oracle8i or
later. Also, XSU contains a PL/SQL wrapper that publishes the XSU Java API to
PL/SQL, creating a PL/SQL API. This way you can:
s
Write new Java applications that run inside the database and that can directly
access the XSU Java API
s
Write PL/SQL applications that access XSU through its PL/SQL API
s
Access XSU functionality directly through SQL
Note: To load and run Java code inside the database you need a
Java-enabled Oracle8i or later server.
XML SQL Utility (XSU) 8-5