WLSKB-009 WORKING WITH XML USING XMLBEAN TUTORIAL

WORKING WITH XML USING XMLBEAN TUTORIAL

INTRODUCTION

XMLBean is an alternative to the SAX and DOM API for parsing XML files. The technology is now publicly available from Apache under the XMLBeans project. Using XMLBean, you can read an update an XML document. Unlike the SAX or DOM API, XMLBean provides a strongly typed and object oriented approach. For example, the <customer> XML element is represented by the Customer class.

The structure of the XML document needs to be formally sepcified in a XML schema document (XSD). Once the XSD file is created, Workshop can generate the necessary Java classes to represent various elements in the schema.

DEVELOP THE SCHEMA

Launch WebLogic Workshop. Create an application called BasicApp, unless it is already created from a previous tutorial. From the menubar, select File->New->Project.

Choose Schema and then Schema Project. Enter MySchemaProject as the project name. Click on Create.

Right click on MySchemaProject and select New->XML Schema. Enter library.xsd as the file name. Click on Create.

We will customize the name space to http://schemas.mycom.com/library.xsd. We will also register a new name space prefix called lib. Make the following changes shown in bold face.

<xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:tns="http://schemas.mycom.com/library.xsd"
    targetNamespace="http://schemas.mycom.com/library.xsd"
    xmlns:lib="http://schemas.mycom.com/library.xsd"
    elementFormDefault="qualified"
    attributeFormDefault="unqualified">

Within the <schema> tag, enter the following schema.

<xs:complexType name="author">
 <xs:sequence>
  <xs:element name="name" type="xs:string"/>
 </xs:sequence>
</xs:complexType>
<xs:complexType name="book">
 <xs:sequence>
  <xs:element name="title" type="xs:string"/>
  <xs:element name="isbn" type="xs:string"/>
  <xs:element name="author" type="lib:author" minOccurs="1" maxOccurs="unbounded"/>
 </xs:sequence>
</xs:complexType>
<!-- A library is a sequence of book elements -->
<xs:element name="library">
 <xs:complexType>
  <xs:sequence>
   <xs:element name="book" type="lib:book" minOccurs="0" maxOccurs="unbounded"/>
  </xs:sequence>
 </xs:complexType>
</xs:element>

Save changes. System will start to build the project and generate the XMLBean Java classes.

Classes are generated in the XML Bean Classes folder as shown above.

USE THE XMLBEAN CLASSES

Within the BasicAppWeb project, create an XML file called library.xml with the following content.

<?xml version="1.0"?> 
     <lib:library xmlns:lib="http://schemas.mycom.com/library.xsd">

 <lib:book>
  <lib:title>2001 Space Oddyssey</lib:title>
  <lib:isbn>0451457994</lib:isbn>
  <lib:author>
   <lib:name>Arthur C. Clarke</lib:name>
  </lib:author>
 </lib:book>
 <lib:book>
  <lib:title>BEA WebLogic Workshop 8.1 Kick Start</lib:title>
  <lib:isbn>0672326221</lib:isbn>
  <lib:author>
   <lib:name>Albert J. Saganich</lib:name>
  </lib:author>
  <lib:author>
   <lib:name>Tom Hardy</lib:name>
  </lib:author>
 </lib:book>
   
</lib:library>

Save and close the file.

Create a new Page Flow called BrowseBookFlow. Open the source code of the flow class. Add the following import statement.

import com.mycom.schemas.library.*;
import com.mycom.schemas.library.LibraryDocument.Library;

Change the implementation of the begin() method to as follows.

protected Forward begin() throws Exception
{
    LibraryDocument doc = LibraryDocument.Factory.parse(
        new java.net.URL("http://localhost:7001/BasicAppWeb/library.xml"));
 
    Library lib = doc.getLibrary();
    Book books[] = lib.getBookArray();
 
    getRequest().setAttribute("books", books);
 
    return new Forward("success");
}

Save changes.

Open index.jsp. Set the contents of the <body> tag to as follows.

<body>
 <h2>Book List</h2>
  <netui-data:repeater dataSource="{request.books}">
   <netui-data:repeaterItem>
    <netui:label value="{container.item.title} - ISBN: {container.item.isbn}"/><br/>
   </netui-data:repeaterItem>
  </netui-data:repeater>
</body>

Save changes.

TEST

Start the server. Open a browser and enter the URL: http://localhost:7001/BasicAppWeb/BrowseBookFlow/BrowseBookFlowController.jpf