Using the XMLBean to Read XML Data
Using an XMLBean to create data is useful, but in terms of what is possible, it is only the tip of the iceberg. The AddressDetailsDocument (or whatever document type is created based on your schema) has the ability to parse XML if it's in the correct format. This instance document is built to the specification of the AddressDetails schema.
<add:Address1>123 Habitat Ring 2</add:Address1>
<add:Address2>Deep Space Nine</add:Address2>
This document, if stored on the hard drive, would be loaded using this code.
String strFileName = "C:\\xmlbeans\\xkit\\schemas\\Laurence\\test.xml";
String strXML = new String();
String strReturn = "";
java.io.File fleInput = new java.io.File(strFileName);
AddressDetailsDocument myParsedDoc =
AddressDetails myParsedDetails = myParsedDoc.getAddressDetails();
strReturn = myParsedDetails.getName();
strReturn = e.toString();
The AddressDetails document has a parse method on its factory that can take xml from a variety of sources. One of these sources is a file, and in the sample code above a file is used.
One the document is initialized you can simply access the information using the getAddressDetails() method to return an AddressDetails object that may be queried for the data.
A Short Note on Data Types
Earlier in the article I mentioned the schema data types as defined by W3C.org. XMLBeans provides data types that match those definitions by providing 46 Java types that mirror the 46 built-in types defined by the XML schema specification. For example, where the W3C define an xs:int, XMLBeans gives you an XmlInt data type.
To access the data using these types you simply use the 'xget' method appropriate to the data that you want. So, in the above examples, if you want the XmlString associated with a name, you would use
There is a performance benefit of using the 'xget' version of the function as the 'get' version has to convert the data to the most appropriate Java type first. For more information on XML data types, please check the XMLBeans documentation.
Accessing the Data Using XQuery and XmlCursors
Another incredibly useful feature that comes with XBeans is its XQuery support. XQuery is a SQL-like language for XML that allows you to query a document to return a result set. It is defined at http://www.w3.org/TR/xquery/. (DevX has several introductory articles on XQuery; see the Related Resources, at left.)
XQuery is very easy to use in XMLBeans, as the Document object supports XmlCursors that can be created by running a query. An example of an XQuery running against your XML document is shown here:
String nsText =
"declare namespace add='http://tempuri.org/AddressDetails";
String pathText = "$this/[add:name='Laurence Moroney']";
String queryText = nsText + queryText;
XmlCursor itemCursor = myParsedDoc.newCursor().execQuery(queryText);
You can then use the XmlCursor to navigate through the nodes that match the query in a similar fashion to a database cursor allowing you to move through a recordset. For more information on this functionality, check the documentation that came with the XMLBeans download.
XML is clearly one of the most important technologies to hit the software industry for many years. It is fantastically powerful, yet amazingly simple. To date, the tools available to Java developers to manipulate XML, while useful, have not been as intuitive as they could be. XMLBeans are a great solution to this problem, completely abstracting the implementation of the document behind a set of beans associated with the schema defining that document.
In addition, they provide many interfaces that allow you to create, parse, and query the data. They do this without impacting the underlying data, and make XML handling fast and easy while reducing the many sources of errors that are inherent in having to code your own logic to handle the contents of XML files.
This article introduced you to some of the basics. The API is now available as open source; you don't need a WebLogic application server to run it. You can use it in your applications and make the important task of processing XML a much easier one!