Java XML Element Text getElementTextOrNull(final XMLStreamReader reader)

Here you can find the source of getElementTextOrNull(final XMLStreamReader reader)

Description

Gets the element text or null if the element has no text.

License

Open Source License

Parameter

Parameter Description
reader the reader (not null)

Return

the text in the element, null if there was no text before the end of the element

Declaration

public static String getElementTextOrNull(final XMLStreamReader reader) throws XMLStreamException 

Method Source Code


//package com.java2s;
// Licensed under the MIT license. See License.txt in the repository root.

import java.text.MessageFormat;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

public class Main {
    /**/*from w w w .ja  v a  2 s  .c om*/
     * Gets the element text or null if the element has no text. The standard
     * {@link XMLStreamReader#getElementText()} implementation never returns
     * null, only the empty string, even when there is no text (empty element
     * <fun/> or <fun></fun>). This method returns null when
     * there are no {@link XMLStreamConstants#CHARACTERS} events before the next
     * {@link XMLStreamConstants#END_ELEMENT} event.
     *
     * @param reader
     *        the reader (not null)
     * @return the text in the element, null if there was no text before the end
     *         of the element
     */
    public static String getElementTextOrNull(final XMLStreamReader reader) throws XMLStreamException {
        /*
         * This implementation is mostly the one recommended in the Javadoc
         * comments on the getElementText() method in the standard API interface
         * itself. However, if no character, cdata, space or entity reference is
         * encountered, the return value is null (not empty string).
         */
        if (reader.getEventType() != XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException("parser must be on START_ELEMENT to read next text", reader.getLocation()); //$NON-NLS-1$
        }

        int eventType = reader.next();
        StringBuffer content = null;
        while (eventType != XMLStreamConstants.END_ELEMENT) {
            if (eventType == XMLStreamConstants.CHARACTERS || eventType == XMLStreamConstants.CDATA
                    || eventType == XMLStreamConstants.SPACE || eventType == XMLStreamConstants.ENTITY_REFERENCE) {
                if (content == null) {
                    content = new StringBuffer();
                }
                content.append(reader.getText());
            } else if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
                    || eventType == XMLStreamConstants.COMMENT) {
                // skipping
            } else if (eventType == XMLStreamConstants.END_DOCUMENT) {
                throw new XMLStreamException("unexpected end of document when reading element text content", //$NON-NLS-1$
                        reader.getLocation());
            } else if (eventType == XMLStreamConstants.START_ELEMENT) {
                throw new XMLStreamException("element text content may not contain START_ELEMENT", //$NON-NLS-1$
                        reader.getLocation());
            } else if (eventType == XMLStreamConstants.ATTRIBUTE) {
                // Skip
            } else {
                final String messageFormat = "Unexpected event type {0}"; //$NON-NLS-1$
                final String message = MessageFormat.format(messageFormat, eventType);
                throw new XMLStreamException(message, reader.getLocation());
            }
            eventType = reader.next();
        }

        return (content == null) ? null : content.toString();
    }
}

Related

  1. getElementText(Element namedElement)
  2. getElementText(Element root, String tagname)
  3. getElementText(final Element element)
  4. getElementTextByTagName(Element n, String elementName)
  5. getElementTextDataNoEx(Element element, boolean unindent)
  6. getElementTextValue(Element element)
  7. getElementTextValue(Element in)
  8. getSimpleElementText(Element element)
  9. getSimpleElementText(Element node)