Example of Parsing XML Using StAX

Using the Streaming API for XML StAX 4-5 2. Use the XMLInputFactory.newInstance method to instantiate an XMLInputFactory, as shown in the following code excerpt: XMLInputFactory xmlif = XMLInputFactory.newInstance; See Section 4.4, Properties Defined for the XMLInputFactory Interface for the list of properties you can set. 3. Use the XMLInputFactory.createXMLStreamReader method to instantiate an XMLStreamReader object based on an XML document. See Section 4.2.2, Getting the XMLStreamReader Object for more details. 4. Parse the XML document, using the hasNext and next methods to step through the XML events, as shown in the following code excerpt: whilexmlr.hasNext{ printEventxmlr; xmlr.next; In the example, xmlr is the XMLStreamReader instance and the local printEvent method not part of the StAX API determines the specific event type, as described in the next step. 5. While parsing the XML document, determine the current specific event type and take appropriate action. Event types include the start and end of an XML document, the start and end of an XML element, comments, entity references, and so on. See Section 4.2.3, Determining the Specific XML Event Type. 6. If the current event type is a start element or end element, optionally get its attributes. See Section 4.2.5, Getting the Attributes of an Element. 7. If the current event type is a start or end element, optionally get its namespaces. See Section 4.2.6, Getting the Namespaces of an Element. 8. If the current event type includes text data, such as a CDATA or comment, optionally get the actual data. See Section 4.2.7, Getting Text Data. 9. Optionally, get location information, such as the line number or column number, of the current event. See Section 4.2.8, Getting Location Information. 10. Close the stream. See Section 4.2.9, Closing the Input Stream.

4.2.1 Example of Parsing XML Using StAX

The following example shows a simple program that uses the XMLStreamReader interface of StAX to parse an XML document. The program takes a single parameter, an XML file, and uses it to create an XMLStreamReader object. The program then uses the reader to iterate over the stream of events, determining the type of each event, such as the start of an XML element, the list of attributes of an element, a processing instruction, and so on. The program prints out information about these events, using internal methods to print out the list of attributes and namespaces when appropriate. 4-6 Programming XML for Oracle WebLogic Server The code in bold is described in later sections. package examples.basic; import java.io.FileReader; import java.util.Iterator; import javax.xml.stream.; import javax.xml.namespace.QName; This is a simple parsing example that illustrates the XMLStreamReader class. author Copyright c 2003 by BEA Systems. All Rights Reserved. public class Parse { private static String filename = null; private static void printUsage { System.out.printlnusage: java examples.basic.Parse xmlfile; } public static void mainString[] args throws Exception { try { filename = args[0]; } catch ArrayIndexOutOfBoundsException aioobe{ printUsage; System.exit0; } Get an input factory XMLInputFactory xmlif = XMLInputFactory.newInstance; System.out.printlnFACTORY: + xmlif; Instantiate a reader XMLStreamReader xmlr = xmlif.createXMLStreamReadernew FileReaderfilename; System.out.printlnREADER: + xmlr + \n; Parse the XML whilexmlr.hasNext{ printEventxmlr; xmlr.next; } Close the reader xmlr.close; } private static void printEventXMLStreamReader xmlr { System.out.printEVENT:[+xmlr.getLocation.getLineNumber+][+ xmlr.getLocation.getColumnNumber+] ; Using the Streaming API for XML StAX 4-7 System.out.print [; switch xmlr.getEventType { case XMLStreamConstants.START_ELEMENT: System.out.print; printNamexmlr; printNamespacesxmlr; printAttributesxmlr; System.out.print; break; case XMLStreamConstants.END_ELEMENT: System.out.print; printNamexmlr; System.out.print; break; case XMLStreamConstants.SPACE: case XMLStreamConstants.CHARACTERS: int start = xmlr.getTextStart; int length = xmlr.getTextLength; System.out.printnew Stringxmlr.getTextCharacters, start, length; break; case XMLStreamConstants.PROCESSING_INSTRUCTION: System.out.print?; if xmlr.hasText System.out.printxmlr.getText; System.out.print?; break; case XMLStreamConstants.CDATA: System.out.print[CDATA[; start = xmlr.getTextStart; length = xmlr.getTextLength; System.out.printnew Stringxmlr.getTextCharacters, start, length; System.out.print]]; break; case XMLStreamConstants.COMMENT: System.out.print--; if xmlr.hasText System.out.printxmlr.getText; System.out.print--; break; case XMLStreamConstants.ENTITY_REFERENCE: System.out.printxmlr.getLocalName+=; if xmlr.hasText System.out.print[+xmlr.getText+]; break; case XMLStreamConstants.START_DOCUMENT: System.out.print?xml; System.out.print version=+xmlr.getVersion+; System.out.print encoding=+xmlr.getCharacterEncodingScheme+; if xmlr.isStandalone 4-8 Programming XML for Oracle WebLogic Server System.out.print standalone=yes; else System.out.print standalone=no; System.out.print?; break; } System.out.println]; } private static void printNameXMLStreamReader xmlr{ ifxmlr.hasName{ String prefix = xmlr.getPrefix; String uri = xmlr.getNamespaceURI; String localName = xmlr.getLocalName; printNameprefix,uri,localName; } } private static void printNameString prefix, String uri, String localName { if uri = null .equalsuri System.out.print[+uri+]:; if prefix = null System.out.printprefix+:; if localName = null System.out.printlocalName; } private static void printAttributesXMLStreamReader xmlr{ for int i=0; i xmlr.getAttributeCount; i++ { printAttributexmlr,i; } } private static void printAttributeXMLStreamReader xmlr, int index { String prefix = xmlr.getAttributePrefixindex; String namespace = xmlr.getAttributeNamespaceindex; String localName = xmlr.getAttributeLocalNameindex; String value = xmlr.getAttributeValueindex; System.out.print ; printNameprefix,namespace,localName; System.out.print=+value+; } private static void printNamespacesXMLStreamReader xmlr{ for int i=0; i xmlr.getNamespaceCount; i++ { printNamespacexmlr,i; } } private static void printNamespaceXMLStreamReader xmlr, int index { String prefix = xmlr.getNamespacePrefixindex; String uri = xmlr.getNamespaceURIindex; System.out.print ; if prefix == null System.out.printxmlns=+uri+; else System.out.printxmlns:+prefix+=+uri+; } } Using the Streaming API for XML StAX 4-9

4.2.2 Getting the XMLStreamReader Object