Example usage for javax.xml.stream XMLStreamReader getAttributeValue

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

Introduction

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

Prototype

public String getAttributeValue(int index);

Source Link

Document

Returns the value of the attribute at the index

Usage

From source file:Main.java

private static void printAttribute(XMLStreamReader xmlr, int index, StringBuffer result) {
    String prefix = xmlr.getAttributePrefix(index);
    String namespace = xmlr.getAttributeNamespace(index);
    String localName = xmlr.getAttributeLocalName(index);
    String value = xmlr.getAttributeValue(index);
    result.append(" ");
    printName(prefix, namespace, localName, result);
    result.append("='").append(value).append("'");
}

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);
    }//from  ww w .  j a  va2s .  c  o m
    return attributeMap;
}

From source file:Main.java

private static void process(XMLStreamReader reader) {
    int eventType = reader.getEventType();
    switch (eventType) {
    case XMLStreamConstants.START_ELEMENT:
        System.out.println("Start element: " + reader.getLocalName());
        int count = reader.getAttributeCount();
        for (int i = 0; i < count; i++) {
            String name = reader.getAttributeLocalName(i);
            String value = reader.getAttributeValue(i);
            System.out.println("\tAttribute name/value: " + name + "/" + value);
        }/*from   w  ww . jav  a  2 s.  c o m*/
        break;
    case XMLStreamConstants.END_ELEMENT:
        System.out.println("End element: " + reader.getLocalName());
        break;

    case XMLStreamConstants.CHARACTERS:
        System.out.println("Text: " + reader.getText());
        break;
    default:
        break;
    }
}

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("\"");
        }/*www . 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:XMLStreamReaderDemo.java

private static void process(XMLStreamReader reader) {
    int eventType = reader.getEventType();
    switch (eventType) {
    case XMLStreamConstants.START_ELEMENT:
        System.out.println("Start element: " + reader.getLocalName());

        int count = reader.getAttributeCount();
        for (int i = 0; i < count; i++) {
            String name = reader.getAttributeLocalName(i);
            String value = reader.getAttributeValue(i);
            System.out.println("\tAttribute name/value: " + name + "/" + value);
        }/*from ww  w.ja  v a 2 s  . c  o  m*/
        break;

    case XMLStreamConstants.END_ELEMENT:
        System.out.println("End element: " + reader.getLocalName());
        break;

    case XMLStreamConstants.CHARACTERS:
        System.out.println("Text: " + reader.getText());
        break;
    default:
        break;
    }
}

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 . java 2  s  . co m
    }

    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 w  w.ja  v a2 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();
    }/*from ww  w . j a  v  a 2 s.co m*/
    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./*from  w w w  .  j a v a2s .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

public static void dumpXML(XMLStreamReader parser) throws XMLStreamException {
    int depth = 0;
    do {/*ww  w . j  a v  a2  s  .  c om*/
        switch (parser.getEventType()) {
        case XMLStreamConstants.START_ELEMENT:
            for (int i = 1; i < depth; ++i) {
                System.out.print("    ");
            }
            System.out.print("<");
            System.out.print(parser.getLocalName());
            for (int i = 0; i < parser.getAttributeCount(); ++i) {
                System.out.print(" ");
                System.out.print(parser.getAttributeLocalName(i));
                System.out.print("=\"");
                System.out.print(parser.getAttributeValue(i));
                System.out.print("\"");
            }
            System.out.println(">");

            ++depth;
            break;
        case XMLStreamConstants.END_ELEMENT:
            --depth;
            for (int i = 1; i < depth; ++i) {
                System.out.print("    ");
            }
            System.out.print("</");
            System.out.print(parser.getLocalName());
            System.out.println(">");
            break;
        case XMLStreamConstants.CHARACTERS:
            for (int i = 1; i < depth; ++i) {
                System.out.print("    ");
            }

            System.out.println(parser.getText());
            break;
        }

        if (depth > 0)
            parser.next();
    } while (depth > 0);
}