Example usage for javax.xml.stream XMLStreamReader getAttributeName

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

Introduction

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

Prototype

public QName getAttributeName(int index);

Source Link

Document

Returns the qname of the attribute at the provided index

Usage

From source file:Main.java

public static void printAttributes(XMLStreamReader xmlr) {
    int count = xmlr.getAttributeCount();

    if (count > 0) {
        for (int i = 0; i < count; i++) {
            System.out.print(" ");
            System.out.print(xmlr.getAttributeName(i).toString());
            System.out.print("=");
            System.out.print("\"");
            System.out.print(xmlr.getAttributeValue(i));
            System.out.print("\"");
        }//from   w w w. j  a v a2 s  .c om
    }

    count = xmlr.getNamespaceCount();

    if (count > 0) {
        for (int i = 0; i < count; i++) {
            System.out.print(" ");
            System.out.print("xmlns");

            if (xmlr.getNamespacePrefix(i) != null) {
                System.out.print(":" + xmlr.getNamespacePrefix(i));
            }

            System.out.print("=");
            System.out.print("\"");
            System.out.print(xmlr.getNamespaceURI(i));
            System.out.print("\"");
        }
    }
}

From source file:Main.java

public static HashMap<String, String> getAttributeMap(XMLStreamReader xmlStreamReader) {

    HashMap<String, String> attributeMap = new HashMap<String, String>();
    int attributeCount = xmlStreamReader.getAttributeCount();
    for (int i = 0; i < attributeCount; i++) {
        String attributeName = xmlStreamReader.getAttributeName(i).getLocalPart();
        String attributeValue = xmlStreamReader.getAttributeValue(i);
        attributeMap.put(attributeName, attributeValue);
    }//  w  w w .j  a v a 2 s . c  om
    return attributeMap;
}

From source file:StaxCursorTest.java

private static void printAttributes(XMLStreamReader xmlr) {
    int count = xmlr.getAttributeCount();

    if (count > 0) {
        for (int i = 0; i < count; i++) {
            System.out.print(" ");
            System.out.print(xmlr.getAttributeName(i).toString());
            System.out.print("=");
            System.out.print("\"");
            System.out.print(xmlr.getAttributeValue(i));
            System.out.print("\"");
        }//from   w w  w . j av a  2s .  com
    }

    count = xmlr.getNamespaceCount();

    if (count > 0) {
        for (int i = 0; i < count; i++) {
            System.out.print(" ");
            System.out.print("xmlns");

            if (xmlr.getNamespacePrefix(i) != null) {
                System.out.print(":" + xmlr.getNamespacePrefix(i));
            }

            System.out.print("=");
            System.out.print("\"");
            System.out.print(xmlr.getNamespaceURI(i));
            System.out.print("\"");
        }
    }
}

From source file:Main.java

private static void printAttributes(XMLStreamReader xmlr) {
    if (xmlr.getAttributeCount() > 0) {
        System.out.println("\nHAS ATTRIBUTES: ");

        int count = xmlr.getAttributeCount();

        for (int i = 0; i < count; i++) {
            QName name = xmlr.getAttributeName(i);
            String namespace = xmlr.getAttributeNamespace(i);
            String type = xmlr.getAttributeType(i);
            String prefix = xmlr.getAttributePrefix(i);
            String value = xmlr.getAttributeValue(i);

            System.out.println("ATTRIBUTE-PREFIX: " + prefix);
            System.out.println("ATTRIBUTE-NAMESP: " + namespace);
            System.out.println("ATTRIBUTE-NAME:   " + name.toString());
            System.out.println("ATTRIBUTE-VALUE:  " + value);
            System.out.println("ATTRIBUTE-TYPE:  " + type);
        }//from  w  ww  .ja v a 2  s .c o  m
    } else {
        System.out.println("HAS NO ATTRIBUTES");
    }
}

From source file:Main.java

public static Map<String, String> attributesToMap(String scope, XMLStreamReader reader) {
    int numAttrs = reader.getAttributeCount();
    if (numAttrs == 0) {
        return Collections.emptyMap();
    }//w  ww  . ja  v  a  2 s .com
    Map<String, String> rtn = new HashMap<String, String>(numAttrs);
    for (int i = 0; i < numAttrs; i++) {
        StringBuilder attrName = new StringBuilder();
        if (scope.length() > 0) {
            attrName.append(scope).append(SCOPE_SEP);
        }
        attrName.append(reader.getAttributeName(i).toString());
        rtn.put(attrName.toString(), reader.getAttributeValue(i));
    }
    return rtn;
}

From source file:Main.java

/**
 * Gets the version of the XML.//w w w.j a  va  2  s.c  om
 *
 * @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:jodtemplate.pptx.io.xml.PresentationXmlRelsReader.java

private Relationship createRelationshipElement(final XMLStreamReader xmlStreamReader) {
    final Relationship relationship = new Relationship();
    for (int index = 0; index < xmlStreamReader.getAttributeCount(); ++index) {
        final String attributeName = xmlStreamReader.getAttributeName(index).getLocalPart();
        final String attributeNS = xmlStreamReader.getAttributeName(index).getNamespaceURI();
        if (OOXMLDocument.ID_ATTRIBUTE.equals(attributeName) && StringUtils.isBlank(attributeNS)) {
            relationship.setId(xmlStreamReader.getAttributeValue(index));
        } else if (OOXMLDocument.TARGET_ATTRIBUTE.equals(attributeName) && StringUtils.isBlank(attributeNS)) {
            relationship.setTarget(xmlStreamReader.getAttributeValue(index));
        } else if (OOXMLDocument.TYPE_ATTRIBUTE.equals(attributeName) && StringUtils.isBlank(attributeNS)) {
            relationship.setType(xmlStreamReader.getAttributeValue(index));
        } else if (OOXMLDocument.TARGET_MODE_ATTRIBUTE.equals(attributeName)
                && StringUtils.isBlank(attributeNS)) {
            relationship.setTargetMode(xmlStreamReader.getAttributeValue(index));
        }/*from  w  w  w. j  a va  2s  . co  m*/
    }
    return relationship;
}

From source file:jodtemplate.io.xml.ContentTypesReader.java

private DefaultElement createDefaultElement(final XMLStreamReader xmlStreamReader) {
    final DefaultElement defaultElement = new DefaultElement();
    for (int index = 0; index < xmlStreamReader.getAttributeCount(); ++index) {
        final String attributeName = xmlStreamReader.getAttributeName(index).getLocalPart();
        final String attributeNS = xmlStreamReader.getAttributeName(index).getNamespaceURI();
        if (OOXMLDocument.CONTENT_TYPE_ATTRIBUTE.equals(attributeName) && StringUtils.isBlank(attributeNS)) {
            defaultElement.setContentType(xmlStreamReader.getAttributeValue(index));
        } else if (OOXMLDocument.EXTENSION_ATTRIBUTE.equals(attributeName)
                && StringUtils.isBlank(attributeNS)) {
            defaultElement.setExtension(xmlStreamReader.getAttributeValue(index));
        }/*from  w  ww  .jav a 2  s.co m*/
    }
    return defaultElement;
}

From source file:jodtemplate.io.xml.ContentTypesReader.java

private OverrideElement createOverrideElement(final XMLStreamReader xmlStreamReader) {
    final OverrideElement overrideElement = new OverrideElement();
    for (int index = 0; index < xmlStreamReader.getAttributeCount(); ++index) {
        final String attributeName = xmlStreamReader.getAttributeName(index).getLocalPart();
        final String attributeNS = xmlStreamReader.getAttributeName(index).getNamespaceURI();
        if (OOXMLDocument.CONTENT_TYPE_ATTRIBUTE.equals(attributeName) && StringUtils.isBlank(attributeNS)) {
            overrideElement.setContentType(xmlStreamReader.getAttributeValue(index));
        } else if (OOXMLDocument.PART_NAME_ATTRIBUTE.equals(attributeName)
                && StringUtils.isBlank(attributeNS)) {
            overrideElement.setPartName(xmlStreamReader.getAttributeValue(index));
        }/*w  w w. j a  v  a2s. c  o  m*/
    }
    return overrideElement;
}

From source file:com.flexive.chemistry.webdav.TextDocumentResource.java

/**
 * Set the value of a property, stream points to the start of the property tag.
 *
 * @param parser    the XML parser// w  ww. j  a  va 2  s  .  c o m
 * @throws XMLStreamException   on parsing errors
 */
protected void processProperty(XMLStreamReader parser) throws XMLStreamException {
    int level = 0;
    String name = null;
    for (int i = 0; i < parser.getAttributeCount(); i++) {
        if ("name".equals(parser.getAttributeName(i).getLocalPart())) {
            name = parser.getAttributeValue(i);
            break;
        }
    }
    if (name == null) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("property without name attribute encountered");
        }
        return;
    }

    String value = null;
    for (int event = parser.nextTag(); event != XMLStreamConstants.END_DOCUMENT
            && level >= 0; event = parser.nextTag()) {
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            if ("value".equals(parser.getLocalName())) {
                value = parser.getElementText().trim();
            } else if ("name".equals(parser.getLocalName())) {
                name = parser.getElementText();
            } else {
                level++;
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            level--;
            break;
        }
    }

    if (value != null) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Setting field " + name + " to " + value);
        }
        try {
            object.setValue(name, value);
        } catch (Exception e) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Failed to set field " + name + " (ignored): " + e.getMessage());
            }
        }
    }
}