background image

Creating the Allocator Method

<< Cursor-to-Event Example | Creating an Input Factory >>
<< Cursor-to-Event Example | Creating an Input Factory >>

Creating the Allocator Method

int eventType = xmlr.getEventType();
while(xmlr.hasNext()){
eventType = xmlr.next();
//Get all
"Book" elements as XMLEvent object
if(eventType == XMLStreamConstants.START_ELEMENT &&
xmlr.getLocalName().equals(
"Book")){
//get immutable XMLEvent
StartElement event = getXMLEvent(xmlr).asStartElement();
System.out.println(
"EVENT: " + event.toString());
}
}
Creating the Allocator Method
The final step is to create the XMLEventAllocator method:
private static XMLEvent getXMLEvent(XMLStreamReader reader)
throws XMLStreamException {
return allocator.allocate(reader);
}
Building and Running the Cursor-to-Event Example Using NetBeans
IDE
Follow these instructions to build and run the Cursor-to-Event example on your Application
Server instance using the NetBeans IDE.
1. In NetBeans IDE, select File
Open Project.
2. In the Open Project dialog, navigate to the tut-install/javaeetutorial5/examples/stax/
directory.
3. Select the cursor2event folder.
4. Select the Open as Main Project check box.
5. Click Open Project Folder.
6. In the Projects tab, right-click the cursor2event project and select Properties. The Project
Properties dialog is displayed.
7. Enter the following in the Arguments field:
BookCatalog.xml
8. Click OK.
9. Right-click the cursor2event project and select Run Project.
Note how the Book events are returned as strings.
Example Code
The Java EE 5 Tutorial · September 2007
572