Java Reference
In-Depth Information
"JavaDatabaseBible.ch17.Xbeans.SerializerBean");
timeBean.setDOMListener(serializer);
serializer.setOutputStream(new FileOutputStream("TimeStamp.xml"));
timeBean.processDocument();
}catch(Exception e){
System.err.println(e);
}
}
}
As you can see from the example in Listing 17-5 , using Xbeans to create and process xml documents is
a simple, two-step process:
1. Instantiate the Xbeans and set any required properties,
2. Call the processDocument() method of the first Xbean in the chain, the SystemTimeBean .
The sequence of events that follows is as follows. The SystemTimeBean first creates a new XML
document using the following code:
Document doc = new DocumentImpl();
It then creates a root element and appends it to the document, as shown here:
Element root = (Element) doc.createElement("SYSTEMDATE");
doc.appendChild (root);
Then it creates elements for year, month, day, and so on, appending them to the root. Here's an
example:
Element year = (Element) doc.createElement("YEAR");
root.appendChild(year);
year.appendChild(doc.createTextNode(""+calendar.get(Calendar.YEAR)));
Finally, it calls the documentReady() method of its registered listener, the SerializerBean , passing
it the new XML document, as shown here:
DOMListener.documentReady(new DOMEvent(this,doc));
The SerializerBean , in turn, calls its own processDocument() method to serialize the document
to the stream defined in its outputStream property. The resulting XML is shown in Listing 17-6 .
Listing 17-6: XML TimeStamp generated and serialized using Xbeans
<?xml version="1.0"?>
<SYSTEMDATE>
<YEAR>2002</YEAR>
<MONTH>3</MONTH>
<DAY>17</DAY>
<DAY_OF_WEEK>1</DAY_OF_WEEK>
<HOUR>15</HOUR>
<MINUTE>43</MINUTE>
</SYSTEMDATE>
Search WWH ::




Custom Search