Files   Prepare   Run   Troubleshooting   Related Topics 

About the Example

This example illustrates how to use the XMLBeans feature to bind data between its XML and Java representations. The example uses the patientSchema.xsd XML Schema file as its starting point.

XMLBeans is an XML<->Java binding tool that uses XML Schema to compile Java interfaces and classes that you can then use to access and modify XML instance data. Using XMLBeans is similar to using any other Java interface/class, you will see things like getFoo or setFoo just as you would expect when working with Java. While a major use of XMLBeans is to access your XML instance data with strongly typed Java classes, there are also API's that allow you access the full XML info set (XMLBeans keeps full XML Info set fidelity) as well as modify the XML schema itself through an XML Schema Object model. See the Apache Web site for more information.

This example first uses the xmlbean Ant task to compile the schema file and create a jar file that contains Java classes based on the patientSchema.xsd's components. The example then uses this jar file in a Servlet to create instances of the xmlBeans based on the schema. Every jar file generated from the xmlbeans Ant task begins with a document class. The name is based on the root element of the XML Schema, concatenated with "Document". In this example, because <medicalRecords> is the root element of patientSchema.xsd, the generated Java class is called MedicalRecordsDocument.

To run the example, the user invokes the xmlBean.jsp JSP in their browser and enters patient information in the provided fields. When the user hits submit, the JSP passes the input values to a servlet (PatientServlet.java). The servlet receives the input values and stores them in member variables in the class to be processed later. The servlet calls the parse() method to create and populate a MedicalRecordsDocument (MRD) instance. The MRD is one of the classes generated by the xmlbean Ant task based on the XML Schema file patientSchema.xsd.

Inside the parse() method, the MRD is populated with the user's input values by invoking setter methods provided by the generated XMLBean classes.

The servlet then sets the MRD object as an attribute and forwards it back to the xmlBean.jsp page. This page receives the MRD object and displays a page with a link to the newly generated patientInfo.xml document. This page also provides a Patient Edit Form that is pre-filled with the user's original values. These fields are populated using the MRD's getter methods generated by XMLBeans. The user can use this form to make changes to the XML document.

Note: Each time the user submits changes to patientInfo.xml, the user should refresh the browser so that the new changes will be displayed properly.


Files Used in the Example

Directory Location:

MW_HOME/wlserver_10.3/samples/server/examples/src/examples/xml/xmlbean/

(where MW_HOME is the directory containing your Oracle WebLogic Server installation)

File

Click source files to view code.

Description

application.xml The Java EE standard enterprise application deployment descriptor.
build.xml Ant build file that contains targets for building and running the example.
ExamplesFooter.jsp Java Server Page containing the Example Footer.
ExamplesHeader.jsp Java Server Page containing the Example Header.
patientSchema.xsd Schema used by the xmlBeans Ant task to generate Java classes for xml processing.
PatientServlet.java Servlet class that processes the user's input values from the form. This servlet uses the generated xmlbean Java classes to to process XML.
PatientXMLValidator.java Java class used to validate generated patientInfo xml file.
web.xml Java EE standard Web application deployment descriptor.
weblogic.xml Oracle WebLogic Server-specific Web application deployment descriptor.
weblogic-application.xml Oracle WebLogic Server-specific enterprise application deployment descriptor.
wls_examples.css Oracle WebLogic Server samples cascade style sheet.
xmlBean.jsp Patient Registration page that calls the PatientServlet class.

Prepare the Example

Prerequisites

Before working with this example:

  1. Install Oracle WebLogic Server, including the examples.
  2. Start the Examples server.
  3. Set up your environment.

Configure Oracle WebLogic Server

  No special configuration is required for this example

Build and Deploy the Example

  1. Change to the SAMPLES_HOME\server\examples\src\examples\xml\xmlbean directory, where SAMPLES_HOME refers to the main Oracle WebLogic Server examples directory, such as d:\Oracle/Middleware\wlserver_10.3\samples.
  2. Execute the following command from the shell where you set your environment:

    ant build

    This command compiles and stages the example.
  3. Execute the following command from the shell where you set your environment:

    ant deploy

    This command deploys the example on the wl_server domain of your Oracle WebLogic Server installation.

The ant command uses the build.xml file, located in the SAMPLES_HOME\server\examples\src\examples\xml\xmlbean directory, to build, package, and deploy the sample application that shows how to use XMLBeans.

Running the build script builds the EJB in the split development directory format where compiled and generated classes are placed in SAMPLES_HOME\server\examples\build\xmlBeanEar directory and non-compiled files remain within the source directory of the example. For more information on the split development directory format, see Developing Applications for Oracle WebLogic Server.


Run the Example

To run the example, follow these steps:

  1. Complete the steps in the "Prepare the Example" section.
  2. In your development shell, run the JSP Tag Handler example by using the following command from the example directory (SAMPLES_HOME\server\examples\src\examples\xml\xmlbean):

    prompt> ant run

    This command executes the build.xml run target, which opens your default browser to the following URL:

    http://localhost:port/xml_xmlBean/xmlBean.jsp

    where

    localhost refers to the machine on which Oracle WebLogic Server is running

    port refers to the port at which Oracle WebLogic Server is listening

    Note: You can open your browser manually and enter the URL above to access the page.

    If your example runs successfully, you will see a JSP page containing a Patient Registration form with twelve data input fields:

 
Social Security
First Name:
Last Name:
DOB: (MM-DD-YYYY)
Gender: Male Female
Email:
Street:
City:
State:
Zip Code:
Country:
Phone:

You may fill in these fields and click on the submit button to use the example. This displays a page with a link to the newly generated patientInfo.xml document and a Patient Edit Form that is pre-filled with the user's original values. These fields are populated using the MRD's getter methods generated by XMLBeans. You can use this form to make changes to the patientInfo.xml document. Changes are committed by altering a field and clicking the submit button. You can view the changes with the link at the top of the resulting page.

Note: to view changes to the patientInfo.xml document, you may need to refresh your browser.


Troubleshooting


Related Topics

(Internet connection required.)


Copyright © 1996, 2009, Oracle and/or its affiliates. All Rights Reserved.