XML Bean
XMLBeans is an XML parser developed by BEA for WebLogic 8.1 and recently released as an open source initiative.
With XMLBeans, you can access and manipulate the data contained in an XML document using Java classes.
XMLBeans is different from the typical XML parser because it is designed to build applications based on known, specific schemas. At its core is a schema compiler that parses an XSD file and creates a JAR. The JAR contains a bunch of Java classes that developers can use to manipulate instance documents as if they were normal Java types.
XMLBeans gives you an object-based view of the underlying XML data, without losing access to the original XML structure.
In addition, it gives you an XQuery parser and XMLCursor objects that allow you to query your documents with the flexibility of a SQL database.
< strong > EXAMPLE : CASE Study < /strong >
STEP 1:
-
-Open a DOS command prompt and run the 'scomp' command. If it runs without error, then congratulations, you have successfully set up XMLBeans.
An example of a very simple schema, which holds name and address details for an individual is shown here.
- Write 'AddressDetails.XSD.':
< ?xml version="1.0" encoding="utf-8" ? >
< xs:schema targetNamespace="http://tempuri.org/AddressDetails.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/AddressDetails.xsd"
xmlns:mstns="http://tempuri.org/AddressDetails.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema" >
< xs:element name="AddressDetails" >
< xs:complexType id="AD" >
< xs:sequence >
< xs:element name="Name" type="xs:string" / >
< xs:element name="Address1" type="xs:string" / >
< xs:element name="Address2" type="xs:string" / >
< xs:element name="ZIP" type="xs:string" / >
< xs:element name="Email" type="xs:string" / >
< /xs:sequence >
< /xs:complexType >
< /xs:element >
< /xs:schema >
This schema is effectively a template describing how your valid XML should appear. It is a simple case where you have an element that should be called AddressDetails containing a number of child elements called Name, Address1, Address2, ZIP, and Email, each of which are defined as strings (xs:string) for validation purposes.
STEP 2:
Open a DOS prompt and change to the directory containing AddressDetails.XSD.
Issue the following command:
Scomp AddressDetails.xsd out 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.
STEP 3:
When writing your Java code, you will need to reference two classes, the AddressDetailsDocument and the AddressDetails.
so the Java file should be some what like:
import org.tempuri.addressDetails.AddressDetailsDocument;
import org.tempuri.addressDetails.AddressDetailsDocument.AddressDetails;
AddressDetailsDocument myDoc;
myDoc = AddressDetailsDocument.Factory.newInstance();
AddressDetails myDetails;
myDetails = myDoc.addNewAddressDetails();
< em > 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 < /em >
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();
Another Example:
If we have a XML file like:
-- text.xml:
< add:AddressDetails xmlns:add="http://tempuri.org/AddressDetails.xsd" >
< add:Name > Laurence Moroney < /add:Name >
< add:Address1 > 123 Habitat Ring 2 < /add:Address1 >
< add:Address2 > Deep Space Nine < /add:Address2 >
< add:ZIP > 11590 < /add:ZIP >
< add:Emailgt;lmoroney@ds9.starfleet.com < /add:Email >
< /add:AddressDetails >
Then the Java could be:
String strFileName = "C:\\xmlbeans\\xkit\\schemas\\Laurence\\test.xml";
String strXML = new String();
String strReturn = "";
java.io.File fleInput = new java.io.File(strFileName);
try
{
AddressDetailsDocument myParsedDoc =
AddressDetailsDocument.Factory.parse(fleInput);
AddressDetails myParsedDetails = myParsedDoc.getAddressDetails();
strReturn = myParsedDetails.getName();
}
catch(Exception e)
{
strReturn = e.toString();
}
return strReturn;
< strong > ADVANCED HINT: < /strong >
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
myParsedDetails.xgetName();
instead of
myParsedDetails.getName();
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!
With XMLBeans, you can access and manipulate the data contained in an XML document using Java classes.
XMLBeans is different from the typical XML parser because it is designed to build applications based on known, specific schemas. At its core is a schema compiler that parses an XSD file and creates a JAR. The JAR contains a bunch of Java classes that developers can use to manipulate instance documents as if they were normal Java types.
XMLBeans gives you an object-based view of the underlying XML data, without losing access to the original XML structure.
In addition, it gives you an XQuery parser and XMLCursor objects that allow you to query your documents with the flexibility of a SQL database.
< strong > EXAMPLE : CASE Study < /strong >
STEP 1:
-
-Open a DOS command prompt and run the 'scomp' command. If it runs without error, then congratulations, you have successfully set up XMLBeans.
An example of a very simple schema, which holds name and address details for an individual is shown here.
- Write 'AddressDetails.XSD.':
< ?xml version="1.0" encoding="utf-8" ? >
< xs:schema targetNamespace="http://tempuri.org/AddressDetails.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/AddressDetails.xsd"
xmlns:mstns="http://tempuri.org/AddressDetails.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema" >
< xs:element name="AddressDetails" >
< xs:complexType id="AD" >
< xs:sequence >
< xs:element name="Name" type="xs:string" / >
< xs:element name="Address1" type="xs:string" / >
< xs:element name="Address2" type="xs:string" / >
< xs:element name="ZIP" type="xs:string" / >
< xs:element name="Email" type="xs:string" / >
< /xs:sequence >
< /xs:complexType >
< /xs:element >
< /xs:schema >
This schema is effectively a template describing how your valid XML should appear. It is a simple case where you have an element that should be called AddressDetails containing a number of child elements called Name, Address1, Address2, ZIP, and Email, each of which are defined as strings (xs:string) for validation purposes.
STEP 2:
Open a DOS prompt and change to the directory containing AddressDetails.XSD.
Issue the following command:
Scomp AddressDetails.xsd out 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.
STEP 3:
When writing your Java code, you will need to reference two classes, the AddressDetailsDocument and the AddressDetails.
so the Java file should be some what like:
import org.tempuri.addressDetails.AddressDetailsDocument;
import org.tempuri.addressDetails.AddressDetailsDocument.AddressDetails;
AddressDetailsDocument myDoc;
myDoc = AddressDetailsDocument.Factory.newInstance();
AddressDetails myDetails;
myDetails = myDoc.addNewAddressDetails();
< em > 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 < /em >
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();
Another Example:
If we have a XML file like:
-- text.xml:
< add:AddressDetails xmlns:add="http://tempuri.org/AddressDetails.xsd" >
< add:Name > Laurence Moroney < /add:Name >
< add:Address1 > 123 Habitat Ring 2 < /add:Address1 >
< add:Address2 > Deep Space Nine < /add:Address2 >
< add:ZIP > 11590 < /add:ZIP >
< add:Emailgt;lmoroney@ds9.starfleet.com < /add:Email >
< /add:AddressDetails >
Then the Java could be:
String strFileName = "C:\\xmlbeans\\xkit\\schemas\\Laurence\\test.xml";
String strXML = new String();
String strReturn = "";
java.io.File fleInput = new java.io.File(strFileName);
try
{
AddressDetailsDocument myParsedDoc =
AddressDetailsDocument.Factory.parse(fleInput);
AddressDetails myParsedDetails = myParsedDoc.getAddressDetails();
strReturn = myParsedDetails.getName();
}
catch(Exception e)
{
strReturn = e.toString();
}
return strReturn;
< strong > ADVANCED HINT: < /strong >
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
myParsedDetails.xgetName();
instead of
myParsedDetails.getName();
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!
Comments