Getting the Full Name of an Element Getting the Attributes of an Element

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

The full name of an element includes its prefix, namespace URI, and local name; use the getPrefix, getNamespaceURI, and getLocalName methods of the XMLStreamReader interface, respectively, to get this information once you determine that the current event is a start or end element. For example, assume the case statement for a start element event in the sample program looks like the following: Using the Streaming API for XML StAX 4-11 case XMLStreamConstants.START_ELEMENT: System.out.print; printNamexmlr; printNamespacesxmlr; printAttributesxmlr; System.out.print; break; The two local printName methods can use the getXXX methods as follows: 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; }

4.2.5 Getting the Attributes of an Element

Once you determine that the current event is a start element, end element, or attribute, use the getAttributeXXX methods of the XMLStreamReader interface to get the list of attributes and their values. Use the getAttributeCount method to return the number of attributes of the current element and use the count in a loop that iterates over the list of attributes. The method does not include namespaces in the count. Additional getAttributeXXX methods return the prefix, namespace URI, local name, and value for a particular attribute. For example, assume the case statement for a start element event in our sample program looks like the following: case XMLStreamConstants.START_ELEMENT: System.out.print; printNamexmlr; printNamespacesxmlr; printAttributesxmlr; Note: The printNamespaces and printAttributes methods are discussed in other sections. Note: You can use the getAttributeXXX methods only on start element, end element, and attribute events; a java.lang.IllegalStateException is thrown if you try to execute the methods on any other type of event. 4-12 Programming XML for Oracle WebLogic Server System.out.print; break; The following local printAttributes method shows one way of iterating through the list of attributes; because attribute indices are zero-based, the for loop starts at 0: private static void printAttributesXMLStreamReader xmlr{ for int i=0; i xmlr.getAttributeCount; i++ { printAttributexmlr,i; } } The following local printAttribute method shows how to print out all the information for a particular attribute: 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+; } The printName method is described in Section 4.2.4, Getting the Full Name of an Element.

4.2.6 Getting the Namespaces of an Element