Example usage for javax.xml.stream XMLEventReader getElementText

List of usage examples for javax.xml.stream XMLEventReader getElementText

Introduction

In this page you can find the example usage for javax.xml.stream XMLEventReader getElementText.

Prototype

public String getElementText() throws XMLStreamException;

Source Link

Document

Reads the content of a text-only element.

Usage

From source file:Main.java

/**
 * 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.//from   www.  j a  v  a2  s.c o m
 * 
 * @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;

}

From source file:com.adobe.acs.commons.it.build.ScrMetadataIT.java

private Descriptor parseScr(InputStream is, String name, boolean checkNs) throws Exception {
    Descriptor result = new Descriptor();

    XMLEventReader reader = xmlInputFactory.createXMLEventReader(is);
    while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();
        if (event.isStartElement()) {
            StartElement start = event.asStartElement();
            String elementName = start.getName().getLocalPart();
            if (elementName.equals("component")) {
                result.name = start.getAttributeByName(new QName("name")).getValue();
                if (checkNs) {
                    String scrUri = start.getName().getNamespaceURI();
                    if (!ALLOWED_SCR_NS_URIS.contains(scrUri)) {
                        throw new Exception(
                                String.format("Banned Namespace URI %s found for %s", scrUri, name));
                    }// w  w w.jav  a  2  s . c  o  m
                }
            } else if (elementName.equals("property")) {
                String propName = start.getAttributeByName(new QName("name")).getValue();
                Attribute value = start.getAttributeByName(new QName("value"));
                Attribute typeAttr = start.getAttributeByName(new QName("type"));
                String type = typeAttr == null ? "String" : typeAttr.getValue();
                if (value != null) {
                    result.properties.add(new Property(propName, value.getValue(), type));
                } else {
                    result.properties.add(new Property(propName, cleanText(reader.getElementText()), type));
                }
            }
        }
    }

    return result;
}

From source file:org.zuinnote.hadoop.office.format.common.parser.msexcel.internal.EncryptedCachedDiskStringsTable.java

/**
 * Parses a string item from a SST in XML format
 * // www  .jav  a2 s  .  c o  m
 * @param xer XMLEventReader from which to read the next string item
 * @throws XMLStreamException           in case the string item cannot be
 *                                      correctly read from the XML file
 * @throws FormatNotUnderstoodException in case a string item cannot be
 *                                      identified in the shared string table
 *                                      (e.g. unknown type)
 */
private String parseSIText(XMLEventReader xer) throws XMLStreamException, FormatNotUnderstoodException {
    String result = "";
    XMLEvent xe;
    while ((xe = xer.nextTag()).isStartElement()) {
        String elementName = xe.asStartElement().getName().getLocalPart().toUpperCase();
        switch (elementName) {
        case "T": // normal text
            result = xer.getElementText();
            break;
        case "R": // rich text (returned as normal text)
            result = this.parseSIRichText(xer);
            break;
        case "RPH": // phonetic (ignored)
        case "PHONETICPR": // phonetic properties (ignored)
            this.skipXMLElementHierarchy(xer);
            break;
        default:
            LOG.error("Unknown string item in shared string table: " + elementName);
            throw new FormatNotUnderstoodException(
                    "Unknown string item in shared string table: " + elementName);

        }
    }
    return result;
}

From source file:org.zuinnote.hadoop.office.format.common.parser.msexcel.internal.EncryptedCachedDiskStringsTable.java

/**
 * Parses a rich text item of a shared string table and returns the unformatted
 * text/*from   w  w  w  . j  ava 2s .  com*/
 * 
 * @param xer
 * @return unformatted text of rich text item
 * @throws FormatNotUnderstoodException
 * @throws XMLStreamException
 */
private String parseSIRichText(XMLEventReader xer) throws XMLStreamException, FormatNotUnderstoodException {
    String result = "";
    XMLEvent xe;
    while ((xe = xer.nextTag()).isStartElement()) {
        String elementName = xe.asStartElement().getName().getLocalPart().toUpperCase();
        switch (elementName) {
        case "T": // normal text
            result = xer.getElementText();
            break;
        case "RPR": // run properties (ignored)
        default:
            LOG.error("Unknown rich text string item in shared string table: " + elementName);
            throw new FormatNotUnderstoodException(
                    "Unknown rich text string item in shared string table: " + elementName);

        }
    }

    return result;
}

From source file:org.zuinnote.hadoop.office.format.common.parser.msexcel.internal.XSSFPullParser.java

/**
 * Parses an inline string from cell in XML format
 * //ww  w  . j av a2s .co m
 * @param xer XMLEventReader from which to read the inline string content
 * @throws XMLStreamException           in case the string item cannot be
 *                                      correctly read from the XML file
 * @throws FormatNotUnderstoodException in case a string cannot be identified in
 *                                      cell
 */
private String parseCellInlineStringText(XMLEventReader xer)
        throws XMLStreamException, FormatNotUnderstoodException {
    String result = "";
    XMLEvent xe;
    while ((xe = xer.nextTag()).isStartElement()) {
        String elementName = xe.asStartElement().getName().getLocalPart().toUpperCase();
        switch (elementName) {
        case "T": // normal text
            result = xer.getElementText();
            break;
        case "R": // rich text (returned as normal text)
            result = this.parseCellInlineStringRichText(xer);
            break;
        case "RPH": // phonetic (ignored)
        case "PHONETICPR": // phonetic properties (ignored)
            this.skipXMLElementHierarchy(xer);
            break;
        default:
            LOG.error("Unknown inline string tag: " + elementName);
            throw new FormatNotUnderstoodException("Unknown inline string tag: " + elementName);

        }
    }
    return result;
}

From source file:org.zuinnote.hadoop.office.format.common.parser.msexcel.internal.XSSFPullParser.java

/**
 * Parses a rich text item of a shared string table and returns the unformatted
 * text// www.  j a v  a2 s . c o m
 * 
 * @param xer
 * @return unformatted text of rich text item
 * @throws FormatNotUnderstoodException
 * @throws XMLStreamException
 */
private String parseCellInlineStringRichText(XMLEventReader xer)
        throws XMLStreamException, FormatNotUnderstoodException {
    String result = "";
    XMLEvent xe;
    while ((xe = xer.nextTag()).isStartElement()) {
        String elementName = xe.asStartElement().getName().getLocalPart().toUpperCase();
        switch (elementName) {
        case "T": // normal text
            result = xer.getElementText();
            break;
        case "RPR": // run properties (ignored)
        default:
            LOG.error("Unknown rich text inline string tag: " + elementName);
            throw new FormatNotUnderstoodException("Unknown rich text inline string tag: " + elementName);

        }
    }

    return result;
}