Perl Cd Bookshelf [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Perl Cd Bookshelf [Electronic resources] - نسخه متنی

Mark V. Scardina, Ben ChangandJinyu Wang

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید








Accessing XML with Java Binding


The JAXB APIs create Java source files from an XML Schema to assist in programmatically creating valid document instances from dynamic data. This is useful when you want an application to send an XML message to another application according to an agreed-upon XML Schema, or you want a web form to construct an XML document. You can construct, optionally validate, and print XML documents that comply with the input XML Schema in your Java applications using the generated classes. The JAXB APIs work in conjunction with the Oracle XML Parser for Java, which parses the XML Schema and passes the parsed XML Schema to the JAXB compiler.


The JAXB APIs then query the XML Schema for all the elements. A Java class is generated for each of these elements. These classes have methods to set the attributes and add child nodes by the corresponding content model. The Java class corresponding to the root element also has methods to validate and print the constructed XML document. The following subsections use an example to show how the JAXB APIs can be used to process an XML Schema and generate classes for the XML Schema’s elements. The example then shows how to use the methods of the element classes to programmatically construct a valid XML document.



The Input XML Schema



The following XML Schema for book data, bookcatalog.xsd, is used as the input to the JAXB Compiler. Here, the schema specifies that the XML document root is BOOKCATALOG. BOOKCATALOG consists of one or more BOOKs. Each BOOK contains a required ISBN attribute as a unique identifier, as well as several optional attributes and child elements, such as TITLE for the book title, AUTHORNAME for the author’s name, PUBLISHER for the publisher, and so on. Optional attributes and children are followed by a ? in the element definition.


<xsd:element name="PRICE" type="xsd:decimal"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>



Generating XML Classes



The following code sample processes an XML Schema and generates the corresponding classes for elements in the DTD. The sample uses an external DTD file along with the name of the root element. The JAXB APIs can also parse an XML document and use the DTD defined in the document. Running the JAXB compiler on the preceding XML Schema creates Java classes for each element (BOOKCATALOG, BOOK, TITLE, and so on). A Java application can then use the methods defined on these classes to create a valid XML document containing book data. The Oracle XDK 10g includes a JAXB compiler that can generate these classes with the following command line:


java -classpath %CLASSPATH% oracle
.xml.jaxb.orajaxb -schema bookcatalog.xsd
-targetPkg generated



Binding to an XML Instance



The following Java code shows how generated methods might be used. Here, two BOOK records are created: book1 and book2. The required attributes are enforced by adding them as parameters to the constructor. In this case, ISBN is a required attribute for the BOOK element. Elements for each column are also created (title1, authorname1, and so on). If an element has an enumerated attribute, a static constant is defined in the class for each value in the enumeration. To build an XML document tree, the various data elements are grouped by assigning them to each row element as tree nodes. Each BOOK element is then added as a node to the document root element BOOKCATALOG. In this example, classes generated are in uppercase.


import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import java.util.List;
public class CreateBooksDemo {
public static void main (String args[]) {
try {
// Instantiate the object factory
CreateBooks.ObjectFactory objFactory = new CreateBooks.ObjectFactory
();
// New Book Catalog
CreateBooks.BOOKCATALOG bookCatalogue =
objFactory.createBOOKCATALOG();
// New book book1
CreateBooks.BOOK book1 = objFactory.createBOOK();
book1.setISBN("7654");
book1.setTITLE("The Adventures of Don Quixote");
book1.setAUTHORNAME("Miguel Cervantes");
book1.setPUBLISHER("Oracle Press");
book1.setPUBLISHYEAR("2000");
book1.setPRICE(new java.math.BigDecimal("1.0"));
// Create new book book2
CreateBooks.BOOK book2 = objFactory.createBOOK();
book2.setISBN("7788");
book2.setTITLE("The Iliad");
book2.setAUTHORNAME("Homer");
book2.setPUBLISHER("Oracle Press");
book2.setPUBLISHYEAR("1000");
book2.setPRICE(new java.math.BigDecimal("2.0"));
// Getting the list
List bookList = bookCatalogue.getBOOK();
// Setting a book item
bookList.add(book1);
bookList.add(book2);
// Get the JAXB Context
JAXBContext jc = JAXBContext.newInstance("CreateBooks");
// Print to System.out
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
m.marshal(bookCatalogue, System.out);
System.out.println();
System.out.println();
}
catch (Exception e) {
e.printStackTrace();
}
}
}



XML Document Created by Java Application



The input for the preceding Java application can be received from various sources, such as a web form, SAX parser, or JDBC result set. The preceding Java application creates an XML document that can then be transformed or stored in the database. The XML document created is similar to the following:


<?xml version="1.0" encoding="UTF-8"?>
<BOOKCATALOG xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="bookcatalog.xsd">
<BOOK>
<TITLE>The Adventures of Don Quixote</TITLE>
<AUTHORNAME>Miguel Cervantes</AUTHORNAME>
<ISBN>7654</ISBN>
<PUBLISHER>Oracle Press</PUBLISHER>
<PUBLISHYEAR>2000</PUBLISHYEAR>
<PRICE>50.00</PRICE>
</BOOK>
<BOOK>
<TITLE>The Iliad</TITLE>
<AUTHORNAME>Homer</AUTHORNAME>
<ISBN>5354</ISBN>
<PUBLISHER>Oracle Press</PUBLISHER>
<PUBLISHYEAR>1000</PUBLISHYEAR>
<PRICE>5.00</PRICE>
</BOOK>
</BOOKCATALOG>


/ 218