Getting the Namespaces of an Element Getting Text Data

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

Once you determine that the current event is a start element, end element, or namespace, use the getNamespaceXXX methods of the XMLStreamReader interface to get the list of namespaces declared for the event. Use the getNamespaceCount method to return the number of namespaces declared for the current event, and use the count in a loop that iterates over the list. If the current event is an end element, the count refers to the number of namespaces that are about to go out of scope. Additional getNamespaceXXX methods return the prefix and namespace URI for a particular namespace. 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; Note: The printName and printNamespaces methods are discussed in other sections. Note: You can use the getNamespaceXXX methods only on start element, end element, and namespace events; a java.lang.IllegalStateException is thrown if you try to execute the methods on any other type of event. Using the Streaming API for XML StAX 4-13 printNamespacesxmlr; printAttributesxmlr; System.out.print; break; The following local printNamespaces method shows one way of iterating through the list of namespaces for the start element; because namespace indices are zero-based, the for loop starts at 0: private static void printNamespacesXMLStreamReader xmlr{ for int i=0; i xmlr.getNamespaceCount; i++ { printNamespacexmlr,i; } } The following local printNamespace method shows how to print out all the information for a particular namespace: 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+; } The getNamespacePrefix method returns null for the default namespace declaration.

4.2.7 Getting Text Data

The XMLStreamReader interface includes various getTextXXX methods for getting text data from events such as comments and CDATA. Use the getTextStart method to get the offset into the text character array where the first character of the current text event is stored. Use the getTextLength method to get the length of the sequence of characters within the text character array. Finally, use the getTextCharacters method to return this character array for the current event. The character array contains text information about only the current event; as soon as you call the next method to read the next event on the input stream, the character array is filled with new information. The following example shows how to print out text data for the CDATA event: case XMLStreamConstants.CDATA: System.out.print[CDATA[; start = xmlr.getTextStart; length = xmlr.getTextLength; System.out.printnew Stringxmlr.getTextCharacters, start, length; System.out.print]]; break; Note: The printName and printAttributes methods are discussed in other sections. 4-14 Programming XML for Oracle WebLogic Server If you want to first check that the character event actually has text, use the hasText method, as shown in the following example: case XMLStreamConstants.COMMENT: System.out.print--; if xmlr.hasText System.out.printxmlr.getText; System.out.print--; break;

4.2.8 Getting Location Information