Java XML QName readTextElement(XMLEventReader reader, QName elemName)

Here you can find the source of readTextElement(XMLEventReader reader, QName elemName)

Description

Reads the text content of an element.

License

Open Source License

Parameter

Parameter Description
reader The event stream from which to read the element text.
elemName The optional name of the element being read. If this paramter is non- <code>null</code> then an exception will be thrown if the element read doesn't have the same name.

Exception

Parameter Description
XMLStreamException If an error occurs reading the stream, or ifthe read element doesn't match the provided QName.

Return

The text read from the element.

Declaration

public static final String readTextElement(XMLEventReader reader, QName elemName) throws XMLStreamException 

Method Source Code

//package com.java2s;

import javax.xml.namespace.QName;

import javax.xml.stream.XMLEventReader;

import javax.xml.stream.XMLStreamException;

import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

public class Main {
    /**/*from   ww  w  .  j a  v  a  2  s.com*/
     * Reads the text content of an element. The reader should be positioned in
     * front of a StartElement event, and will be read up to and including the
     * end element tag.
     * 
     * @param reader The event stream from which to read the element text.
     * @param elemName The optional name of the element being read. If this
     *            paramter is non- <code>null</code> then an exception will
     *            be thrown if the element read doesn't have the same name.
     * @return The text read from the element.
     * @throws XMLStreamException If an error occurs reading the stream, or if
     *             the read element doesn't match the provided QName.
     */
    public static final String readTextElement(XMLEventReader reader, QName elemName) throws XMLStreamException {

        if (elemName != null) {

            requireStartElement(reader, elemName);

        }

        // read text
        String text = reader.getElementText();

        // consume the end tag
        reader.nextEvent();

        return text;

    }

    /**
     * Utility method that throws an exception if the provided reader is not
     * positioned before a StartElement event with the specified tag name.
     * 
     * @param reader The reader to test.
     * @param qname The required name of the start-tag. If <code>null</code>,
     *            any start tag is accepted.
     * @throws XMLStreamException If an error occurs reading from the stream.
     */
    public static final void requireStartElement(XMLEventReader reader, QName qname) throws XMLStreamException {

        if (reader.hasNext()) {

            XMLEvent nextEvent = reader.peek();
            if (nextEvent.isStartElement()) {

                if (qname != null) {

                    StartElement start = nextEvent.asStartElement();
                    QName name = start.getName();
                    if (!name.equals(qname)) {

                        throw new XMLStreamException(
                                "Encountered unexpected element; expected " + qname + ", but found " + name);

                    }

                }

            } else {

                throw new XMLStreamException("Encountered unexpected event; expected " + qname
                        + " start-tag, but found event " + nextEvent);

            }

        } else {

            throw new XMLStreamException("Encountered unexpected end of stream; expected element " + qname);

        }

    }
}

Related

  1. namedNode(QName qname)
  2. nodeToQName(Node node)
  3. parseXml(final Node node, final Map properties)
  4. printPath(List path)
  5. read(Node node, String expression, QName returnType)
  6. resolveNamespace(String qname, Node xml)
  7. resolveQName(final Element el, final String qualifiedName)
  8. resolveQName(String qNameWithPrefix, Element element)
  9. search(List list, Element baseElement, QName nodeName, boolean recursive)