Page 1 of 1

Reading XML using XMLBeans Rate Topic: -----

#1 bhandari  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 754
  • Joined: 31-January 08

Posted 06 March 2008 - 02:28 AM

Attached File  person.txt (16.6K)
Number of downloads: 867Hi,

Here we will be using XMLBeans to parse and then read its contents. As an example consider a person database.
We will have a person group which will contain many persons and each person has a name and age. The XML representing this structure is as:


Save the above as person.xml

A XSD (XML Schema Diagram) is a general representation of the XML file. The XSD representing the above XML is as below:

<xs:schema xmlns:xs="">
	<xs:element name="person-group">
				<xs:element name="person" type="person"/>
	   <xs:complexType name="person">
			   <xs:element name="name" type="xs:string"/>
			   <xs:element name="age" type="xs:int"/>

Save the above as person.xsd

XMLBeans will work on our person.xsd to generate Java Beans using which we can read/write XML.

Download XMLBeans from here, extract the contenets and include bin and lib folders in CLASSPATH environment variable as you do for Java SDK.

Now go to command prompt and issue the command as:

E:\scomp –out e:\person.jar –compiler c:\j2sdk\bin\javac e:\person.xsd

Note: scomp is a tool available in XMLBeans’ bin folder.
The above command will generate person.jar in e:. You can also download it from this post as I have attached it alongwith. (Change person.txt to person.jar). Include the above generated person.jar in the CLASSPATH.

Now we need to write a program to parse and then read our person.xml file. The code to do that is reproduced here:
import org.apache.xmlbeans.XmlCursor;
import noNamespace.PersonGroupDocument;

public class GeneratePage {
	public static void main(String args[]) {
		XmlCursor cursor = null;
		try {
			String filePath = "e:\\person.xml";
			File inputXMLFile = new File(filePath);
			PersonGroupDocument persGrpDoc = PersonGroupdocument.Factory.parse(inputXMLFile);
			PersonGroupdocument.PersonGroup prsGrp = persGrpDoc.getPersonGroup();
			cursor = prsGrp.newCursor();
		} catch (Exception e) {
		} finally {

Lets now understand the above code:
1) First we create a file instant
2) We create an object to represent whole of the document. This object will contain whole of the document which we get by calling parse method as Factory.Parse(inputXMLFile);
3) From the document object we get the root element of the document which is personGroup. The persGrpDoc has a method to get the root element which is generate by the scomp tool.
4) Now getting the root element, we get a cursor over the root element by calling newCursor() method on the root element.
5) Now we need to iterate the cursor to print the value of each node. But fortunately we don’t need to do so. The getTextValue() method of the XMLCursor does the job for us and we put the result of this method in println() method.

The advatages of XMLBeans over DOM/SAX parsers are:
1) DOM parsers create an instance of whole of the document in one go which may eat up memory if document is large. XMLBeans does incremental unmarshalling of XML and hence uses memory only when required.
2) SAX parsers can’t write to XML and moreover the developer needs to write event handlers.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1