Example usage for javax.xml.stream XMLStreamReader getLocalName

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

Introduction

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

Prototype

public String getLocalName();

Source Link

Document

Returns the (local) name of the current event.

Usage

From source file:Main.java

/**
 * Advance the reader to the next element after start element and return true. 
 * Return false if next start element is not found
 * @param reader a XMLStreamReader/*w w  w .  j  a v a 2s . com*/
 * @param startElement Name of the start element.
 * @return True when successfully advanced the reader.
 * @throws XMLStreamException Exception when reading from the XMLStreamReader fails.
 */
public static boolean advanceToAfterStartElement(XMLStreamReader reader, String startElement)
        throws XMLStreamException {
    while (!(reader.getEventType() == XMLStreamConstants.START_ELEMENT && reader.hasName()
            && reader.getLocalName().equalsIgnoreCase(startElement))) {
        //String name = (reader.hasName()? reader.getLocalName() : "");
        if (reader.hasNext()) {
            reader.next();
        } else {
            // reach the end of elements in reader. Not found.
            return false;
        }
    }
    // found the startElement. Consume that start element also
    if (reader.hasNext()) {
        reader.next();
        return true;
    }

    return false;
}

From source file:Main.java

public static boolean skipToEndOfElement(XMLStreamReader in, String localName) throws XMLStreamException {
    for (int code = in.getEventType(); code != XMLStreamReader.END_DOCUMENT; code = in.next()) {
        if (code == XMLStreamReader.END_ELEMENT && (localName == null || localName.equals(in.getLocalName()))) {
            return true;
        }// w  w w.  j  av  a  2  s.  c o  m
    }
    return false;
}

From source file:Main.java

public static boolean skipToStartOfElement(XMLStreamReader in, String localName) throws XMLStreamException {
    for (int code = in.getEventType(); code != XMLStreamReader.END_DOCUMENT; code = in.next()) {
        if (code == XMLStreamReader.START_ELEMENT
                && (localName == null || localName.equals(in.getLocalName()))) {
            return true;
        }/*  w w w  .  ja v  a 2 s . c  o m*/
    }
    return false;
}

From source file:Main.java

private static void parseRestOfDocument(XMLStreamReader reader) throws XMLStreamException {

    while (reader.hasNext()) {
        int type = reader.next();
        switch (type) {
        case XMLStreamConstants.START_ELEMENT:
            System.out.println(reader.getLocalName());
            if (reader.getNamespaceURI() != null) {
                String prefix = reader.getPrefix();
                if (prefix == null) {
                    prefix = "[None]";
                }//from w  w w .  j  a v  a2  s  . com
                System.out.println("prefix = '" + prefix + "', URI = '" + reader.getNamespaceURI() + "'");
            }

            if (reader.getAttributeCount() > 0) {
                for (int i = 0; i < reader.getAttributeCount(); i++) {
                    System.out.println("Attribute (name = '" + reader.getAttributeLocalName(i) + "', value = '"
                            + reader.getAttributeValue(i) + "')");
                    String attURI = reader.getAttributeNamespace(i);
                    if (attURI != null) {
                        String attPrefix = reader.getAttributePrefix(i);
                        if (attPrefix == null || attPrefix.equals("")) {
                            attPrefix = "[None]";
                        }
                        System.out.println("prefix=" + attPrefix + ",URI=" + attURI);
                    }
                }
            }

            break;
        case XMLStreamConstants.END_ELEMENT:
            System.out.println("XMLStreamConstants.END_ELEMENT");
            break;
        case XMLStreamConstants.CHARACTERS:
            if (!reader.isWhiteSpace()) {
                System.out.println("CD:" + reader.getText());
            }
            break;
        case XMLStreamConstants.DTD:
            System.out.println("DTD:" + reader.getText());
            break;
        case XMLStreamConstants.SPACE:
            System.out.println(" ");
            break;
        case XMLStreamConstants.COMMENT:
            System.out.println(reader.getText());
            break;
        default:
            System.out.println(type);
        }
    }
}

From source file:it.govpay.core.utils.client.SOAPUtils.java

public static Object unmarshal(InputStream is, Schema schema)
        throws JAXBException, SAXException, IOException, XMLStreamException {

    XMLStreamReader xsr = xif.createXMLStreamReader(is);

    boolean isBody = false;
    while (!isBody) {
        xsr.nextTag();//from  w w w.  j av  a2s .  co  m
        if (xsr.isStartElement()) {
            String local = xsr.getLocalName();
            isBody = local.equals("Body");
        }
    }

    xsr.nextTag();
    if (!xsr.isStartElement()) {
        // Body vuoto
        return null;
    } else {
        return JaxbUtils.unmarshal(xsr, schema);
    }
}

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 w  ww  .j av  a 2  s. c  om
                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

/**
 * Gets the version of the XML./*from   w ww .  j  av  a2 s.  c  o  m*/
 *
 * @param path the XML file.
 * @return the corresponding version of the XML.
 */
public static String getXMLVersion(final Path path) {
    try (InputStream inputStream = Files.newInputStream(path)) {
        XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
        for (int event; (event = reader.next()) != XMLStreamConstants.END_DOCUMENT;) {
            if (event == XMLStreamConstants.START_ELEMENT) {
                String tmp = reader.getLocalName();
                if ("persistence".equals(tmp)) {
                    for (int i = 0; i < reader.getAttributeCount(); i++) {
                        if ("version".equals(reader.getAttributeName(i).toString())) {
                            return reader.getAttributeValue(i);
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
        throw new RuntimeException("Error reading the persistence.xml version.", ex);
    }
    return null;
}

From source file:Main.java

/**
 * Borrowed from org.apache.xml.security.test.stax.utils.XmlReaderToWriter
 *//*from   w  ww. jav a  2 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: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
 * @return true if the required values are matched, false otherwise
 *//*from ww w.j a v a  2s.c o m*/
private static boolean isElement(XMLStreamReader reader, int event, String uri, String name) {
    if (reader.getEventType() != event)
        return false;

    if (uri != null) {
        String found = reader.getNamespaceURI();
        if (!found.equals(uri))
            return false;
    }

    if (name != null) {
        String found = reader.getLocalName();
        if (!found.equals(name))
            return false;
    }

    return true;
}

From source file:Main.java

public static String parseValue(XMLStreamReader xmlRdr, String elementName) throws XMLStreamException {
    String val = null;
    while (true) {
        xmlRdr.next();/*from  w w w .java 2 s  .  c om*/
        int event2 = xmlRdr.getEventType();
        if (event2 == XMLStreamConstants.CHARACTERS) {
            val = xmlRdr.getText();
        } else if (event2 == XMLStreamConstants.END_ELEMENT && xmlRdr.getLocalName().equals(elementName)) {
            return val;
        }
    }
}