Example usage for javax.xml.stream XMLStreamReader getText

List of usage examples for javax.xml.stream XMLStreamReader getText

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamReader getText.

Prototype

public String getText();

Source Link

Document

Returns the current value of the parse event as a string, this returns the string value of a CHARACTERS event, returns the value of a COMMENT, the replacement value for an ENTITY_REFERENCE, the string value of a CDATA section, the string value for a SPACE event, or the String value of the internal subset of the DTD.

Usage

From source file:com.cedarsoft.serialization.test.performance.XmlParserPerformance.java

private void benchParse(javolution.xml.stream.XMLInputFactory inputFactory)
        throws XMLStreamException, javolution.xml.stream.XMLStreamException {
    for (int i = 0; i < BIG; i++) {
        javolution.xml.stream.XMLStreamReader parser = inputFactory
                .createXMLStreamReader(new StringReader(CONTENT_SAMPLE));

        assertEquals(XMLStreamReader.START_ELEMENT, parser.nextTag());
        assertEquals("fileType", parser.getLocalName().toString());

        boolean dependent = Boolean.parseBoolean(parser.getAttributeValue(null, "dependent").toString());

        assertEquals(XMLStreamReader.START_ELEMENT, parser.nextTag());
        assertEquals("id", parser.getLocalName().toString());
        assertEquals(XMLStreamReader.CHARACTERS, parser.next());

        String id = parser.getText().toString();

        assertEquals(XMLStreamReader.END_ELEMENT, parser.nextTag());
        assertEquals("id", parser.getLocalName().toString());

        assertEquals(XMLStreamReader.START_ELEMENT, parser.nextTag());
        assertEquals("extension", parser.getLocalName().toString());

        boolean isDefault = Boolean.parseBoolean(parser.getAttributeValue(null, "default").toString());
        String delimiter = parser.getAttributeValue(null, "delimiter").toString();

        assertEquals(XMLStreamReader.CHARACTERS, parser.next());

        String extension = parser.getText().toString();

        assertEquals(XMLStreamReader.END_ELEMENT, parser.nextTag());
        assertEquals("extension", parser.getLocalName().toString());

        assertEquals(XMLStreamReader.END_ELEMENT, parser.nextTag());
        assertEquals("fileType", parser.getLocalName().toString());
        assertEquals(XMLStreamReader.END_DOCUMENT, parser.next());

        parser.close();/*  w  w w . j a v a 2  s . c  om*/

        FileType type = new FileType(id, new Extension(delimiter, extension, isDefault), dependent);
        assertNotNull(type);
    }
}

From source file:com.cedarsoft.serialization.test.performance.XmlParserPerformance.java

private void benchParse(XMLInputFactory inputFactory, @Nonnull String contentSample) throws XMLStreamException {
    for (int i = 0; i < BIG; i++) {
        XMLStreamReader parser = inputFactory.createXMLStreamReader(new StringReader(contentSample));

        assertEquals(XMLStreamReader.START_ELEMENT, parser.nextTag());
        assertEquals("fileType", parser.getLocalName());
        assertEquals("fileType", parser.getName().getLocalPart());

        boolean dependent = Boolean.parseBoolean(parser.getAttributeValue(null, "dependent"));

        assertEquals(XMLStreamReader.START_ELEMENT, parser.nextTag());
        assertEquals("id", parser.getName().getLocalPart());
        assertEquals(XMLStreamReader.CHARACTERS, parser.next());

        String id = parser.getText();

        assertEquals(XMLStreamReader.END_ELEMENT, parser.nextTag());

        assertEquals(XMLStreamReader.START_ELEMENT, parser.nextTag());
        assertEquals("extension", parser.getName().getLocalPart());

        boolean isDefault = Boolean.parseBoolean(parser.getAttributeValue(null, "default"));
        String delimiter = parser.getAttributeValue(null, "delimiter");

        assertEquals(XMLStreamReader.CHARACTERS, parser.next());

        String extension = parser.getText();
        assertEquals(XMLStreamReader.END_ELEMENT, parser.nextTag());
        assertEquals("extension", parser.getName().getLocalPart());

        assertEquals(XMLStreamReader.END_ELEMENT, parser.nextTag());
        assertEquals("fileType", parser.getName().getLocalPart());
        assertEquals(XMLStreamReader.END_DOCUMENT, parser.next());

        parser.close();//  ww w .j  a v a 2  s. co m

        FileType type = new FileType(id, new Extension(delimiter, extension, isDefault), dependent);
        assertNotNull(type);
    }
}

From source file:net.landora.animeinfo.mylistreader.ListReader.java

public boolean download(InputStream input) throws Throwable {
    TarInputStream is = null;/*from w w w . ja  v a 2s  .  co  m*/
    try {
        is = new TarInputStream(new GZIPInputStream(input));

        TarEntry entry;
        while ((entry = is.getNextEntry()) != null) {
            if (!entry.getName().equalsIgnoreCase("mylist.xml")) {
                continue;
            }

            XMLStreamReader reader = XMLInputFactory.newFactory().createXMLStreamReader(is);
            reader.nextTag();
            reader.require(XMLStreamReader.START_ELEMENT, null, "my_anime_list");
            values = new HashMap<String, String>();
            StringBuilder value = new StringBuilder();

            while (reader.nextTag() != XMLStreamReader.END_ELEMENT) {
                reader.require(XMLStreamReader.START_ELEMENT, null, null);
                String tableName = reader.getLocalName();

                values.clear();

                while (reader.nextTag() != XMLStreamReader.END_ELEMENT) {
                    String valueName = reader.getLocalName();

                    value.setLength(0);
                    while (reader.next() != XMLStreamReader.END_ELEMENT) {
                        switch (reader.getEventType()) {
                        case XMLStreamReader.CDATA:
                        case XMLStreamReader.CHARACTERS:
                        case XMLStreamReader.SPACE:
                            value.append(reader.getText());
                        }
                    }
                    reader.require(XMLStreamReader.END_ELEMENT, null, valueName);
                    values.put(valueName, value.toString());

                }
                reader.require(XMLStreamReader.END_ELEMENT, null, tableName);

                handleTable(tableName);

            }
            reader.require(XMLStreamReader.END_ELEMENT, null, "my_anime_list");

            saveLast();
        }
        return true;
    } finally {
        if (is != null) {
            IOUtils.closeQuietly(is);
        } else if (input != null) {
            IOUtils.closeQuietly(input);
        }
    }
}

From source file:net.landora.anidb.mylist.ListReader.java

public boolean download(InputStream input) throws Throwable {
    TarInputStream is = null;// w w  w .ja  va2s  .  co m
    try {
        is = new TarInputStream(new GZIPInputStream(input));

        TarEntry entry;
        while ((entry = is.getNextEntry()) != null) {
            if (!entry.getName().equalsIgnoreCase("mylist.xml")) {
                continue;
            }

            XMLStreamReader reader = XMLInputFactory.newFactory().createXMLStreamReader(is);
            reader.nextTag();
            reader.require(XMLStreamReader.START_ELEMENT, null, "my_anime_list");
            values = new HashMap<>();
            StringBuilder value = new StringBuilder();

            while (reader.nextTag() != XMLStreamReader.END_ELEMENT) {
                reader.require(XMLStreamReader.START_ELEMENT, null, null);
                String tableName = reader.getLocalName();

                values.clear();

                while (reader.nextTag() != XMLStreamReader.END_ELEMENT) {
                    String valueName = reader.getLocalName();

                    value.setLength(0);
                    while (reader.next() != XMLStreamReader.END_ELEMENT) {
                        switch (reader.getEventType()) {
                        case XMLStreamReader.CDATA:
                        case XMLStreamReader.CHARACTERS:
                        case XMLStreamReader.SPACE:
                            value.append(reader.getText());
                        }
                    }
                    reader.require(XMLStreamReader.END_ELEMENT, null, valueName);
                    values.put(valueName, value.toString());

                }
                reader.require(XMLStreamReader.END_ELEMENT, null, tableName);

                handleTable(tableName);

            }
            reader.require(XMLStreamReader.END_ELEMENT, null, "my_anime_list");

            saveLast();
        }
        return true;
    } finally {
        if (is != null) {
            IOUtils.closeQuietly(is);
        } else if (input != null) {
            IOUtils.closeQuietly(input);
        }
    }
}

From source file:davmail.exchange.ews.EWSMethod.java

protected String handleTag(XMLStreamReader reader, String localName) throws XMLStreamException {
    String result = null;//  w  w w.j  a va 2 s .co m
    int event = reader.getEventType();
    if (event == XMLStreamConstants.START_ELEMENT && localName.equals(reader.getLocalName())) {
        while (reader.hasNext()
                && !((event == XMLStreamConstants.END_ELEMENT && localName.equals(reader.getLocalName())))) {
            event = reader.next();
            if (event == XMLStreamConstants.CHARACTERS) {
                result = reader.getText();
            }
        }
    }
    return result;
}

From source file:Main.java

/**
 * Borrowed from org.apache.xml.security.test.stax.utils.XmlReaderToWriter
 *//*from  ww w. j a v  a2  s.  c o m*/
public static void write(XMLStreamReader xmlr, XMLStreamWriter writer) throws XMLStreamException {
    switch (xmlr.getEventType()) {
    case XMLEvent.START_ELEMENT:
        final String localName = xmlr.getLocalName();
        final String namespaceURI = xmlr.getNamespaceURI();
        if (namespaceURI != null && namespaceURI.length() > 0) {
            final String prefix = xmlr.getPrefix();
            if (prefix != null) {
                writer.writeStartElement(prefix, localName, namespaceURI);
            } else {
                writer.writeStartElement(namespaceURI, localName);
            }
        } else {
            writer.writeStartElement(localName);
        }

        for (int i = 0, len = xmlr.getNamespaceCount(); i < len; i++) {
            String prefix = xmlr.getNamespacePrefix(i);
            if (prefix == null) {
                writer.writeDefaultNamespace(xmlr.getNamespaceURI(i));
            } else {
                writer.writeNamespace(prefix, xmlr.getNamespaceURI(i));
            }
        }

        for (int i = 0, len = xmlr.getAttributeCount(); i < len; i++) {
            final String attUri = xmlr.getAttributeNamespace(i);

            if (attUri != null && attUri.length() > 0) {
                final String prefix = xmlr.getAttributePrefix(i);
                if (prefix != null) {
                    writer.writeAttribute(prefix, attUri, xmlr.getAttributeLocalName(i),
                            xmlr.getAttributeValue(i));
                } else {
                    writer.writeAttribute(attUri, xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i));
                }
            } else {
                writer.writeAttribute(xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i));
            }

        }
        break;
    case XMLEvent.END_ELEMENT:
        writer.writeEndElement();
        break;
    case XMLEvent.SPACE:
    case XMLEvent.CHARACTERS:
        char[] text = new char[xmlr.getTextLength()];
        xmlr.getTextCharacters(0, text, 0, xmlr.getTextLength());
        writer.writeCharacters(text, 0, text.length);
        break;
    case XMLEvent.PROCESSING_INSTRUCTION:
        writer.writeProcessingInstruction(xmlr.getPITarget(), xmlr.getPIData());
        break;
    case XMLEvent.CDATA:
        writer.writeCData(xmlr.getText());
        break;
    case XMLEvent.COMMENT:
        writer.writeComment(xmlr.getText());
        break;
    case XMLEvent.ENTITY_REFERENCE:
        writer.writeEntityRef(xmlr.getLocalName());
        break;
    case XMLEvent.START_DOCUMENT:
        String encoding = xmlr.getCharacterEncodingScheme();
        String version = xmlr.getVersion();

        if (encoding != null && version != null) {
            writer.writeStartDocument(encoding, version);
        } else if (version != null) {
            writer.writeStartDocument(xmlr.getVersion());
        }
        break;
    case XMLEvent.END_DOCUMENT:
        writer.writeEndDocument();
        break;
    case XMLEvent.DTD:
        writer.writeDTD(xmlr.getText());
        break;
    }
}

From source file:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.ddi.DDIFileReader.java

private String getElementText(XMLStreamReader xmlr) throws XMLStreamException {
    if (xmlr.getEventType() != XMLStreamConstants.START_ELEMENT) {
        throw new XMLStreamException("parser must be on START_ELEMENT to read next text", xmlr.getLocation());
    }/*from  w w  w. j  a  v a2  s  .  com*/
    int eventType = xmlr.next();
    StringBuffer content = new StringBuffer();
    while (eventType != XMLStreamConstants.END_ELEMENT) {
        if (eventType == XMLStreamConstants.CHARACTERS || eventType == XMLStreamConstants.CDATA
                || eventType == XMLStreamConstants.SPACE
        /* || eventType == XMLStreamConstants.ENTITY_REFERENCE*/) {
            content.append(xmlr.getText());
        } else if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
                || eventType == XMLStreamConstants.COMMENT
                || eventType == XMLStreamConstants.ENTITY_REFERENCE) {
            // skipping
        } else if (eventType == XMLStreamConstants.END_DOCUMENT) {
            throw new XMLStreamException("unexpected end of document when reading element text content");
        } else if (eventType == XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException("element text content may not contain START_ELEMENT",
                    xmlr.getLocation());
        } else {
            throw new XMLStreamException("Unexpected event type " + eventType, xmlr.getLocation());
        }
        eventType = xmlr.next();
    }
    return content.toString();
}

From source file:com.liferay.portal.util.LocalizationImpl.java

private void _copyNonExempt(XMLStreamReader xmlStreamReader, XMLStreamWriter xmlStreamWriter,
        String exemptLanguageId, String defaultLanguageId, boolean cdata) throws XMLStreamException {

    while (xmlStreamReader.hasNext()) {
        int event = xmlStreamReader.next();

        if (event == XMLStreamConstants.START_ELEMENT) {
            String languageId = xmlStreamReader.getAttributeValue(null, _LANGUAGE_ID);

            if (Validator.isNull(languageId)) {
                languageId = defaultLanguageId;
            }//from   w w  w . j  a va2  s  .co  m

            if (!languageId.equals(exemptLanguageId)) {
                xmlStreamWriter.writeStartElement(xmlStreamReader.getLocalName());
                xmlStreamWriter.writeAttribute(_LANGUAGE_ID, languageId);

                while (xmlStreamReader.hasNext()) {
                    event = xmlStreamReader.next();

                    if ((event == XMLStreamConstants.CHARACTERS) || (event == XMLStreamConstants.CDATA)) {

                        String text = xmlStreamReader.getText();

                        if (cdata) {
                            xmlStreamWriter.writeCData(text);
                        } else {
                            xmlStreamWriter.writeCharacters(xmlStreamReader.getText());
                        }

                        break;
                    } else if (event == XMLStreamConstants.END_ELEMENT) {
                        break;
                    }
                }

                xmlStreamWriter.writeEndElement();
            }
        } else if (event == XMLStreamConstants.END_DOCUMENT) {
            break;
        }
    }
}

From source file:XmlStreamUtils.java

public static String printEvent(XMLStreamReader xmlr) {
    StringBuffer b = new StringBuffer();
    b.append("EVENT:[" + xmlr.getLocation().getLineNumber() + "][" + xmlr.getLocation().getColumnNumber()
            + "] ");
    b.append(getName(xmlr.getEventType()));
    b.append(" [");
    switch (xmlr.getEventType()) {
    case XMLStreamReader.START_ELEMENT:
        b.append("<");
        printName(xmlr, b);//from   www  .  j av a 2 s.c  om
        for (int i = 0; i < xmlr.getNamespaceCount(); i++) {
            b.append(" ");
            String n = xmlr.getNamespacePrefix(i);
            if ("xmlns".equals(n)) {
                b.append("xmlns=\"" + xmlr.getNamespaceURI(i) + "\"");
            } else {
                b.append("xmlns:" + n);
                b.append("=\"");
                b.append(xmlr.getNamespaceURI(i));
                b.append("\"");
            }
        }

        for (int i = 0; i < xmlr.getAttributeCount(); i++) {
            b.append(" ");
            printName(xmlr.getAttributePrefix(i), xmlr.getAttributeNamespace(i), xmlr.getAttributeLocalName(i),
                    b);
            b.append("=\"");
            b.append(xmlr.getAttributeValue(i));
            b.append("\"");
        }

        b.append(">");
        break;
    case XMLStreamReader.END_ELEMENT:
        b.append("</");
        printName(xmlr, b);
        for (int i = 0; i < xmlr.getNamespaceCount(); i++) {
            b.append(" ");
            String n = xmlr.getNamespacePrefix(i);
            if ("xmlns".equals(n)) {
                b.append("xmlns=\"" + xmlr.getNamespaceURI(i) + "\"");
            } else {
                b.append("xmlns:" + n);
                b.append("=\"");
                b.append(xmlr.getNamespaceURI(i));
                b.append("\"");
            }
        }
        b.append(">");
        break;
    case XMLStreamReader.SPACE:
    case XMLStreamReader.CHARACTERS:
        // b.append(xmlr.getText());
        int start = xmlr.getTextStart();
        int length = xmlr.getTextLength();
        b.append(new String(xmlr.getTextCharacters(), start, length));
        break;
    case XMLStreamReader.PROCESSING_INSTRUCTION:
        String target = xmlr.getPITarget();
        if (target == null)
            target = "";
        String data = xmlr.getPIData();
        if (data == null)
            data = "";
        b.append("<?");
        b.append(target + " " + data);
        b.append("?>");
        break;
    case XMLStreamReader.CDATA:
        b.append("<![CDATA[");
        if (xmlr.hasText())
            b.append(xmlr.getText());
        b.append("]]&gt;");
        break;

    case XMLStreamReader.COMMENT:
        b.append("<!--");
        if (xmlr.hasText())
            b.append(xmlr.getText());
        b.append("-->");
        break;
    case XMLStreamReader.ENTITY_REFERENCE:
        b.append(xmlr.getLocalName() + "=");
        if (xmlr.hasText())
            b.append("[" + xmlr.getText() + "]");
        break;
    case XMLStreamReader.START_DOCUMENT:
        b.append("<?xml");
        b.append(" version='" + xmlr.getVersion() + "'");
        b.append(" encoding='" + xmlr.getCharacterEncodingScheme() + "'");
        if (xmlr.isStandalone())
            b.append(" standalone='yes'");
        else
            b.append(" standalone='no'");
        b.append("?>");
        break;

    }
    b.append("]");
    return b.toString();
}

From source file:com.marklogic.client.functionaltest.BasicJavaClientREST.java

/**
 * Convert XMLStreamReader To String/*www  .ja v a2  s. com*/
 * @param XMLStreamReader
 * @return String
 * @throws XMLStreamException, TransformerException, IOException, ParserConfigurationException, SAXException
 */
public String convertXMLStreamReaderToString(XMLStreamReader reader) throws XMLStreamException,
        TransformerException, IOException, ParserConfigurationException, SAXException {
    String str = null;
    while (reader.hasNext()) {
        reader.next();
        int a = reader.getEventType();
        if (reader.hasText())
            if (reader.getText() != "null")
                str = str + reader.getText().trim();
    }
    return str;
}