Example usage for javax.xml.stream XMLStreamReader getAttributeCount

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

Introduction

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

Prototype

public int getAttributeCount();

Source Link

Document

Returns the count of attributes on this START_ELEMENT, this method is only valid on a START_ELEMENT or ATTRIBUTE.

Usage

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

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("\"");
        }/* w  w w. j  a v a2  s. c o 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, StringBuffer result) {
    for (int i = 0; i < xmlr.getAttributeCount(); i++) {
        printAttribute(xmlr, i, result);
    }// www . j a  va 2s.c  om
}

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);
        }// w w w  .jav  a2 s  .c  om
    } else {
        System.out.println("HAS NO ATTRIBUTES");
    }
}

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("\"");
        }// w w w.j  a  v a  2  s.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

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  va 2 s.  co  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  . j  ava 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: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 w  ww  .ja v a  2s . 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

/**
 * Gets the version of the XML.//from w  w  w . j  av  a  2s  .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 {//from w w  w .j ava 2  s.co  m
        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);
}