Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

XML Made Easy with XMLBeans : Page 2

A new XML parser from BEA makes it easy to handle instance documents in Java, using regular bean-like syntax.


advertisement
Building Your First XMLBean
Before you begin you will of course need the XMLBeans development kit from BEA. The downloadable ZIP file includes everything you need to get started. Download it from http://dev2dev.bea.com/technologies/xmlbeans/overview.jsp. Unzip the file and you will get a directory structure as in Figure 1.

To install the development kit, you must follow these steps.

  • If you don't have it already, download and install a version of Java 2 Standard edition SDK version 1.4 or later from http://java.sun.com.
  • Make sure that J2SE is installed and that the JAVA_HOME environment variable points to the installation. JAVA_HOME/bin should contain at least java.exe, javac.exe, and jar.exe for XMLBeans to work properly.
  • Create an XMLBEANDIR environment variable and point it to the directory containing xbean.jar. In the example from Figure 1, this value would be C:\xmlbeans\xkit\lib
  • Add the XMLBeans 'bin' directory to your path (example: C:\xmlbeans\xkit\bin).
Figure 1. Unzipped: The default directory structure in the XMLBeans SDK is shown.
Open a DOS command prompt and run the 'scomp' command. If it runs without error, then congratulations, you have successfully set up XMLBeans.

When you have reached this stage, building your first XMLBean is very simple. In the 'Schemas' directory, create a new subdirectory called AddressDetails. Take the AddressDetails.XSD from the download for this article and put it in this directory.



Open a DOS prompt and change to the directory containing AddressDetails.XSD.

Issue the following command:

Scomp AddressDetails.xsd out AddressDetails.jar

If all goes well you should see this message:

Loading schema file AddressDetails.xsd Time to build schema type system: 0.851 seconds Time to generate code: 1.983 seconds Compiled types to AddressDetails.jar

You will now have a JAR file containing the classes necessary to process or create XML documents that are defined by the schema.

Using the XMLBean to Create XML
Creating XML with the XMLBean is very easy. You simply create instances of the object that represents your data and add them to the object that represents your document. It's far easier than it sounds.

If you are using an IDE to create your Java file, you will have to add references to the jar file containing the XMLBeans (that you just created), and to the xbean.jar, which is in the \xmlbeans\xkit\lib directory.

When writing your Java code, you will need to reference two classes, the AddressDetailsDocument and the AddressDetails.

import org.tempuri.addressDetails.AddressDetailsDocument; import org.tempuri.addressDetails.AddressDetailsDocument.AddressDetails;

Author's Note: The documentation that comes with XMLBeans has a walkthrough that uses a purchase order schema and demonstrates some sample code to allow you to process purchase orders. There are some bugs in this code, particularly with the imports, and these can be misleading.

One you have imported the relevant classes, you can create XML. This is simply a matter of creating an instance of the relevant document and the relevant section within the document using the appropriate classes.

AddressDetailsDocument myDoc; myDoc = AddressDetailsDocument.Factory.newInstance(); AddressDetails myDetails; myDetails = myDoc.addNewAddressDetails();

Your document is created by the AddressDetailsDocument factory, calling it with the newInstance() method. You can then use the addNewAddressDetails() method on this object to create a new AddressDetails section.

This demonstrates the usefulness of the code that XMLBeans has generated for you. Instead of dealing with generic names, or having to write custom code to manage the data items that you want, the methods on the beans are named appropriately and coded for you.

To add data to the AddressDetails section, you simply call the appropriate 'set' methods. These methods should look familiar—they directly map to the element names defined in the schema.

myDetails.setName("Laurence Moroney"); myDetails.setAddress1("123 Beverly Hills"); myDetails.setAddress2("Los Angeles, CA"); myDetails.setEmail("lmoroney@philotic.com"); myDetails.setZIP("11590");

The XMLBean also provides you with 'get' methods that may be used to read the date, for example:

String strName = myDetails.getName();

To create more, you can continually use the addNewAddressDetails method on the AddressDetailsDocument object.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap