Getting the XMLStreamReader Object Determining the Specific XML Event Type

Using the Streaming API for XML StAX 4-9

4.2.2 Getting the XMLStreamReader Object

Use the XMLInputFactory.createXMLStreamReader method to instantiate an XMLStreamReader object based on an XML document, as shown in the following code excerpt: XMLStreamReader xmlr = xmlif.createXMLStreamReadernew FileReaderfilename; In the example, xmlif is the XMLInputFactory instance. The various signatures of the createXMLStreamReader method allow for the following XML document formats as parameters: ■ java.io.InputStream ■ java.io.Reader shown in the example ■ javax.xml.transform.Source specified in the JAXP API http:java.sun.comxml

4.2.3 Determining the Specific XML Event Type

To determine the specific event type while parsing an XML document, use either the XMLStreamReader.next or XMLStreamReader.getEventType methods. The next method reads the next event and returns an integer which identifies the read event type; the getEventType method simply returns the integer identifying the current event type. The XMLStreamConstants superinterface of XMLStreamReader defines the event type constants, shown in the following list: ■ XMLStreamConstants.ATTRIBUTE ■ XMLStreamConstants.CDATA ■ XMLStreamConstants.CHARACTERS ■ XMLStreamConstants.COMMENT ■ XMLStreamConstants.DTD ■ XMLStreamConstants.END_DOCUMENT ■ XMLStreamConstants.END_ELEMENT ■ XMLStreamConstants.ENTITY_DECLARATION ■ XMLStreamConstants.ENTITY_REFERENCE ■ XMLStreamConstants.NAMESPACE ■ XMLStreamConstants.NOTATION_DECLARATION ■ XMLStreamConstants.PROCESSING_INSTRUCTION ■ XMLStreamConstants.SPACE ■ XMLStreamConstants.START_DOCUMENT ■ XMLStreamConstants.START_ELEMENT The following example shows how to use the Java case statement to determine the particular type of event that was returned by the XMLStreamReader.next method. The example uses the XMLStreamReader.getEventType method to determine the integer event type of the current event returned by the next method. For simplicity, the example simply prints that an event has been found; later sections show further processing of the event. switch xmlr.getEventType { 4-10 Programming XML for Oracle WebLogic Server case XMLStreamConstants.START_ELEMENT: System.out.printStart Element\n; break; case XMLStreamConstants.END_ELEMENT: System.out.printEnd Element\n; break; case XMLStreamConstants.SPACE: System.out.printSpace\n; break; case XMLStreamConstants.CHARACTERS: System.out.printCharacters\n; break; case XMLStreamConstants.PROCESSING_INSTRUCTION: System.out.printProcessing Instrcutions\n; break; case XMLStreamConstants.CDATA: System.out.printCDATA\n; break; case XMLStreamConstants.COMMENT: System.out.printComment\n; break; case XMLStreamConstants.DTD: System.out.printDTD\n; break; case XMLStreamConstants.ENTITY_REFERENCE: System.out.printEntity Reference\n; break; case XMLStreamConstants.ENTITY_DECLARATION: System.out.printEntity Declaration\n; break; case XMLStreamConstants.START_DOCUMENT: System.out.printStart Document\n; break; case XMLStreamConstants.END_DOCUMENT: System.out.printEnd Document\n; break; }

4.2.4 Getting the Full Name of an Element