Example usage for javax.xml.stream XMLStreamReader getEventType

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

Introduction

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

Prototype

public int getEventType();

Source Link

Document

Returns an integer code that indicates the type of the event the cursor is pointing to.

Usage

From source file:Main.java

/**
 * Skips a tag or subtree./*  ww  w.  j a va 2  s.c o m*/
 */
public static void skip(XMLStreamReader parser) throws XMLStreamException {
    assert parser != null;

    int level = 1;
    while (next(parser)) {
        int event = parser.getEventType();
        if (event == XMLStreamConstants.START_ELEMENT) {
            level++;
        } else if (event == XMLStreamConstants.END_ELEMENT) {
            level--;
            if (level == 0) {
                break;
            }
        }
    }

    next(parser);
}

From source file:XmlReaderToWriter.java

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/*from www  .jav a  2  s . c o m*/
                writer.writeStartElement(namespaceURI, localName);
        } else {
            writer.writeStartElement(localName);
        }

        for (int i = 0, len = xmlr.getNamespaceCount(); i < len; i++) {
            writer.writeNamespace(xmlr.getNamespacePrefix(i), xmlr.getNamespaceURI(i));
        }

        for (int i = 0, len = xmlr.getAttributeCount(); i < len; i++) {
            String attUri = xmlr.getAttributeNamespace(i);
            if (attUri != null)
                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:
        writer.writeCharacters(xmlr.getTextCharacters(), xmlr.getTextStart(), xmlr.getTextLength());
        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:Main.java

/**
 * Test if the current event is an element tag with the given namespace and name.
 * If the namespace URI is null it is not checked for equality,
 * if the local name is null it is not checked for equality.
 * @param reader must not be {@code null}
 * @param event START_ELEMENT or END_ELEMENT
 * @param uri the namespace URI of the element, may be null
 * @param name the local name of the element, may be null
 * @param slash "" or "/", for error message
 * @throws XMLStreamException if the required values are not matched.
 *//*w ww.  ja  v a 2  s . c  om*/
private static void requireElement(XMLStreamReader reader, int event, String uri, String name, String slash)
        throws XMLStreamException {
    // Note: reader.require(event, uri, name) has a lousy error message

    if (reader.getEventType() != event)
        throw new XMLStreamException("expected <" + slash + name + ">", reader.getLocation());

    if (uri != null) {
        String found = reader.getNamespaceURI();
        if (!found.equals(uri))
            throw new XMLStreamException(
                    "expected <" + slash + name + "> with namespace [" + uri + "], found [" + found + "]",
                    reader.getLocation());
    }

    if (name != null) {
        String found = reader.getLocalName();
        if (!found.equals(name))
            throw new XMLStreamException("expected <" + slash + name + ">, found <" + slash + found + ">",
                    reader.getLocation());
    }
}

From source file:Main.java

/**
 * Borrowed from org.apache.xml.security.test.stax.utils.XmlReaderToWriter
 *//*from  w  w  w.j  a  va  2s . c om*/
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:net.cloudkit.enterprises.ws.SuperPassQueryTest.java

public static String parsingReceiptStatus(String responseContext) throws XMLStreamException {
    String serviceResponseCode = "-1";
    XMLStreamReader reader = factory.createXMLStreamReader(new StringReader(responseContext));
    try {/*from ww  w  .j  a  va 2 s .  c  om*/
        int event = reader.getEventType();
        while (true) {
            switch (event) {
            case XMLStreamConstants.START_ELEMENT:
                // System.out.println(reader.getName());
                if (reader.getName().toString().equals("ServiceResponseCode")) {
                    // System.out.println(reader.getElementText());
                    serviceResponseCode = reader.getElementText();
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                // System.out.println("End Element:" + r.getName());
                break;
            }
            if (!reader.hasNext())
                break;
            event = reader.next();
        }
    } finally {
        reader.close();
    }
    return serviceResponseCode;
}

From source file:Main.java

public static String parse(XMLStreamReader reader) {
    StringBuffer result = new StringBuffer();
    if (reader != null) {
        try {/*from   ww w .  j  a  v a2 s .  c  o  m*/
            while (reader.hasNext()) {
                switch (reader.getEventType()) {

                case XMLStreamConstants.START_ELEMENT:
                    result.append("<");
                    printName(reader, result);
                    printNamespaces(reader, result);
                    printAttributes(reader, result);
                    result.append(">");
                    break;

                case XMLStreamConstants.END_ELEMENT:
                    result.append("</");
                    printName(reader, result);
                    result.append(">");
                    break;

                case XMLStreamConstants.SPACE:

                case XMLStreamConstants.CHARACTERS:
                    int start = reader.getTextStart();
                    int length = reader.getTextLength();
                    result.append(new String(reader.getTextCharacters(), start, length));
                    break;

                case XMLStreamConstants.PROCESSING_INSTRUCTION:
                    result.append("<?");
                    if (reader.hasText())
                        result.append(reader.getText());
                    result.append("?>");
                    break;

                case XMLStreamConstants.CDATA:
                    result.append("<![CDATA[");
                    start = reader.getTextStart();
                    length = reader.getTextLength();
                    result.append(new String(reader.getTextCharacters(), start, length));
                    result.append("]]>");
                    break;

                case XMLStreamConstants.COMMENT:
                    result.append("<!--");
                    if (reader.hasText())
                        result.append(reader.getText());
                    result.append("-->");
                    break;

                case XMLStreamConstants.ENTITY_REFERENCE:
                    result.append(reader.getLocalName()).append("=");
                    if (reader.hasText())
                        result.append("[").append(reader.getText()).append("]");
                    break;

                case XMLStreamConstants.START_DOCUMENT:
                    result.append("<?xml");
                    result.append(" version='").append(reader.getVersion()).append("'");
                    result.append(" encoding='").append(reader.getCharacterEncodingScheme()).append("'");
                    if (reader.isStandalone())
                        result.append(" standalone='yes'");
                    else
                        result.append(" standalone='no'");
                    result.append("?>");
                    break;
                }
                reader.next();
            } // end while
        } catch (XMLStreamException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                reader.close();
            } catch (XMLStreamException e) {
            }
        }
    }
    return result.toString();
}

From source file:net.cloudkit.enterprises.ws.SuperPassQueryTest.java

public static String parsingReceiptData(String responseData) throws XMLStreamException {
    StringBuilder dataBuilder = new StringBuilder("");
    XMLStreamReader reader = factory.createXMLStreamReader(new StringReader(responseData));
    try {//from w  ww . j  a  v  a2  s. c  om
        int event = reader.getEventType();
        while (true) {
            switch (event) {
            case XMLStreamConstants.START_ELEMENT:

                if (reader.getName().toString().equals("TradeName")) {
                    // System.out.println(reader.getElementText());
                    dataBuilder.append(reader.getElementText());
                    dataBuilder.append("\t");
                }
                // System.out.println(reader.getName());
                if (reader.getName().toString().equals("SeqNo")) {
                    // System.out.println(reader.getElementText());
                    dataBuilder.append(reader.getElementText());
                    dataBuilder.append("\t");
                }
                // BillNo
                if (reader.getName().toString().equals("EntryId")) {
                    // System.out.println(reader.getElementText());
                    dataBuilder.append(reader.getElementText());
                    dataBuilder.append("\t");
                }
                if (reader.getName().toString().equals("IEFlag")) {
                    // System.out.println(reader.getElementText());
                    dataBuilder.append(reader.getElementText());
                    dataBuilder.append("\t");
                }
                // ??
                if (reader.getName().toString().equals("IEDate")) {
                    // System.out.println(reader.getElementText());
                    dataBuilder.append(reader.getElementText());
                    dataBuilder.append("\t");
                }
                if (reader.getName().toString().equals("TradeMode")) {
                    // System.out.println(reader.getElementText());
                    dataBuilder.append(reader.getElementText());
                    dataBuilder.append("\t");
                }
                if (reader.getName().toString().equals("Status")) {
                    // System.out.println(reader.getElementText());
                    dataBuilder.append(reader.getElementText());
                    dataBuilder.append("\t");
                }
                if (reader.getName().toString().equals("RetExplain")) {
                    // System.out.println(reader.getElementText());
                    dataBuilder.append(reader.getElementText());
                    dataBuilder.append("\t");
                }
                // NoticeDate
                // CustomsCode
                break;
            case XMLStreamConstants.END_ELEMENT:
                // System.out.println("End Element:" + r.getName());
                break;
            }
            if (!reader.hasNext())
                break;
            event = reader.next();
        }
    } finally {
        reader.close();
    }
    dataBuilder.append("\n");
    return dataBuilder.toString();
}

From source file:fr.inria.oak.paxquery.pact.operators.unary.NavigationOperator.java

/**
 * /*w ww . j a v a 2s  . c om*/
 * @param record
 * @param treePattern
 * @param collector
 */
public static void navigation(NestedMetadata inputRecordSignature, Record record, int column,
        NavigationTreePattern navigationTreePattern, Collector<Record> collector) {
    XMLInputFactory factory = XMLInputFactory.newInstance();

    try {
        StringValue value = record.getField(column, StringValue.class);
        XMLStreamReader streamReader = factory.createXMLStreamReader(new StringReader(value.getValue()));

        SingleDocumentExtractor extractor = new SingleDocumentExtractor(navigationTreePattern, streamReader);

        while (streamReader.hasNext()) {
            streamReader.next();
            if (streamReader.getEventType() == XMLStreamConstants.START_ELEMENT) {
                extractor.startElement();
            } else if (streamReader.getEventType() == XMLStreamConstants.END_ELEMENT) {
                extractor.endElement();
            } else if (streamReader.getEventType() == XMLStreamConstants.CHARACTERS) {
                extractor.characters();
            }

            if (extractor.getRecords().size() != 0) {
                Iterator<Record> pactRecordsIterator = extractor.getRecords().iterator();
                while (pactRecordsIterator.hasNext()) {
                    Record originalRecord = record.createCopy();
                    RecordOperations.concatenate(originalRecord, pactRecordsIterator.next());
                    collector.collect(originalRecord);
                }

                extractor.getRecords().clear();
            }
        }

    } catch (XMLStreamException e) {
        logger.error("XMLStreamException", e);
    }
}

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);/* ww w  .  j  av  a2s  .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:edu.utah.further.core.api.xml.XmlUtil.java

/**
 * Basic StAX element printout. For more sophisticated functionality, use
 * {@link XmlStreamPrinter}./*from ww w .ja  v a2 s  .c o m*/
 * 
 * @param reader
 *            reader
 * @return reader's next element textual representation
 */
public static String getEventSimpleString(final XMLStreamReader reader) {
    switch (reader.getEventType()) {
    case XMLStreamConstants.START_ELEMENT:
        return "START_ELEMENT:\t\"" + reader.getLocalName() + "\"";
    case XMLStreamConstants.END_ELEMENT:
        return "END_ELEMENT:\t\"" + reader.getLocalName() + "\"";
    case XMLStreamConstants.START_DOCUMENT:
        return "START_DOCUMENT";
    case XMLStreamConstants.END_DOCUMENT:
        return "END_DOCUMENT";
    case XMLStreamConstants.CHARACTERS:
        return "CHARACTERS:\t\"" + reader.getText() + "\"" + " blank? "
                + StringUtils.isWhitespace(reader.getText());
    case XMLStreamConstants.SPACE:
        return "SPACE:\t\"" + reader.getText() + "\"";
    default:
        return "EVENT:\t" + reader.getEventType();
    }
}