Example usage for javax.xml.stream.events Attribute getValue

List of usage examples for javax.xml.stream.events Attribute getValue

Introduction

In this page you can find the example usage for javax.xml.stream.events Attribute getValue.

Prototype

public String getValue();

Source Link

Document

Gets the normalized value of this attribute

Usage

From source file:XMLEventReaderDemo.java

public static void main(String[] args) throws Exception {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    Reader fileReader = new FileReader("Source.xml");
    XMLEventReader reader = factory.createXMLEventReader(fileReader);

    while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();
        if (event.isStartElement()) {
            StartElement element = (StartElement) event;
            System.out.println("Start Element: " + element.getName());

            Iterator iterator = element.getAttributes();
            while (iterator.hasNext()) {
                Attribute attribute = (Attribute) iterator.next();
                QName name = attribute.getName();
                String value = attribute.getValue();
                System.out.println("Attribute name/value: " + name + "/" + value);
            }/*w  w  w .j  a  v  a2s . co m*/
        }
        if (event.isEndElement()) {
            EndElement element = (EndElement) event;
            System.out.println("End element:" + element.getName());
        }
        if (event.isCharacters()) {
            Characters characters = (Characters) event;
            System.out.println("Text: " + characters.getData());
        }
    }
}

From source file:org.eclipse.swordfish.core.configuration.xml.SaxParsingPrototype.java

/**
 * @param args/* ww  w  . ja v a  2 s.  co  m*/
 */
public static void main(String[] args) throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    LinkedList<String> currentElements = new LinkedList<String>();
    InputStream in = SaxParsingPrototype.class.getResource("ComplexPidXmlProperties.xml").openStream();
    XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
    Map<String, List<String>> props = new HashMap<String, List<String>>();
    // Read the XML document
    while (eventReader.hasNext()) {
        XMLEvent event = eventReader.nextEvent();
        if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) {
            putElement(props, getQualifiedName(currentElements), event.asCharacters().getData());

        } else if (event.isStartElement()) {
            System.out.println(event.asStartElement().getName());
            currentElements.add(event.asStartElement().getName().getLocalPart());
            for (Iterator attrIt = event.asStartElement().getAttributes(); attrIt.hasNext();) {
                Attribute attribute = (Attribute) attrIt.next();
                putElement(props, getQualifiedName(currentElements) + "[@" + attribute.getName() + "]",
                        attribute.getValue());

            }
        } else if (event.isAttribute()) {
        } else if (event.isEndElement()) {
            String lastElem = event.asEndElement().getName().getLocalPart();
            if (!currentElements.getLast().equals(lastElem)) {
                throw new UnsupportedOperationException(lastElem + "," + currentElements.getLast());
            }
            currentElements.removeLast();
        }
    }

    eventReader.close();
}

From source file:Main.java

public static String getAttributeValue(StartElement element, String namespaceURI, String localPart) {
    QName name = new QName(namespaceURI == null ? XMLConstants.NULL_NS_URI : namespaceURI, localPart);
    Attribute attribute = element.getAttributeByName(name);
    return attribute == null ? null : attribute.getValue();
}

From source file:Main.java

public static int getIntElement(StartElement startElement, String elementName) {
    int parent = 0;
    Attribute parentIdAttr = startElement.getAttributeByName(new QName(elementName));
    if (parentIdAttr != null) {
        parent = Integer.parseInt(parentIdAttr.getValue());
    }//  ww  w  .j  a va 2 s  . co m
    return parent;
}

From source file:Main.java

public static boolean attributesEqual(Attribute attribute1, Attribute attribute2) {
    if (!attribute1.getName().equals(attribute2.getName())) {
        return false;
    } else if (!attribute1.getValue().equals(attribute2.getValue())) {
        return false;
    }//from  w  w  w.  jav a  2 s  .  c  o  m
    return true;
}

From source file:Main.java

/**
 * //from  w  w w.  j a  v a 2  s .  c o m
 * @param elementName
 * @param attributeValue
 * @param is
 * @return Collection
 * @throws XMLStreamException
 * @throws FactoryConfigurationError
 * @throws UnsupportedEncodingException
 */
public static Collection<String> getElementValues(final String elementName, final String attributeValue,
        final InputStream is)
        throws XMLStreamException, UnsupportedEncodingException, FactoryConfigurationError {
    final Collection<String> elementValues = new ArrayList<>();
    final XMLEventReader xmlEventReader = XMLInputFactory.newInstance()
            .createXMLEventReader(new InputStreamReader(is, Charset.defaultCharset().name()));
    final StringBuffer characters = new StringBuffer();
    boolean read = false;

    while (xmlEventReader.peek() != null) {
        final XMLEvent event = (XMLEvent) xmlEventReader.next();

        switch (event.getEventType()) {
        case XMLStreamConstants.START_DOCUMENT:
        case XMLStreamConstants.END_DOCUMENT: {
            // Ignore.
            break;
        }
        case XMLStreamConstants.START_ELEMENT: {
            read = elementName.equals(event.asStartElement().getName().getLocalPart());

            if (read && attributeValue != null) {
                read = false;

                for (Iterator<Attribute> iterator = event.asStartElement().getAttributes(); iterator
                        .hasNext();) {
                    Attribute attribute = iterator.next();

                    if (attribute.getValue().equals(attributeValue)) {
                        read = true;
                        break;
                    }
                }
            }

            break;
        }
        case XMLStreamConstants.CHARACTERS: {
            if (read) {
                characters.append(event.asCharacters().getData());
            }
            break;
        }
        case XMLStreamConstants.END_ELEMENT: {
            if (read) {
                elementValues.add(characters.toString());
                characters.setLength(0);
            }

            read = false;
            break;
        }
        default: {
            // Ignore
            break;
        }
        }
    }

    return elementValues;
}

From source file:com.predic8.membrane.core.interceptor.rest.XML2HTTP.java

/**
 * @return the attribute's value or null, if the element has no attribute with the given name.
 *///from   w ww .  ja va 2 s  . c o m
private static String getAttribute(StartElement element, String name) throws XML2HTTPException {
    Attribute attribute = element.getAttributeByName(new QName(name));
    if (attribute == null)
        return null;
    return attribute.getValue();
}

From source file:HTTPChunkLocator.java

private static String get(final StartElement element, final QName name) throws IOException {
    final Attribute attributeByName = element.getAttributeByName(name);
    if (attributeByName == null) {
        throw new IOException("missing attribute " + name.toString());
    }// w  w w .ja v  a 2  s. c  o m
    return attributeByName.getValue();
}

From source file:com.predic8.membrane.core.interceptor.rest.XML2HTTP.java

/**
 * @return the attribute's value//from   w ww . jav a  2s.c o m
 * @throws XML2HTTPException if no such attribute exists
 */
private static String requireAttribute(StartElement element, String name) throws XML2HTTPException {
    Attribute attribute = element.getAttributeByName(new QName(name));
    if (attribute == null)
        throw new XML2HTTPException("XML-HTTP doc <" + element.getName().getLocalPart()
                + "> element does not have @" + name + " attribute.");
    return attribute.getValue();
}

From source file:com.hp.mqm.clt.xml.JunitXmlIterator.java

@Override
protected void onEvent(XMLEvent event) throws IOException {
    if (event instanceof StartElement) {
        StartElement element = (StartElement) event;
        String localName = element.getName().getLocalPart();
        if ("testcase".equals(localName)) { // NON-NLS
            packageName = "";
            className = "";
            testName = "";
            status = TestResultStatus.PASSED;
            duration = 0;/*  www .ja v  a 2s .c o  m*/

            Iterator iterator = element.getAttributes();
            while (iterator.hasNext()) {
                Attribute attribute = (Attribute) iterator.next();
                if ("classname".equals(attribute.getName().toString())) {
                    parseClassname(attribute.getValue());
                } else if ("name".equals(attribute.getName().toString())) {
                    testName = attribute.getValue();
                } else if ("time".equals(attribute.getName().toString())) {
                    duration = parseTime(attribute.getValue());
                }
            }
        } else if ("skipped".equals(localName)) { // NON-NLS
            status = TestResultStatus.SKIPPED;
        } else if ("failure".equals(localName)) { // NON-NLS
            // This should cover the rerunFailure as well
            status = TestResultStatus.FAILED;
        } else if ("error".equals(localName)) { // NON-NLS
            status = TestResultStatus.FAILED;
        }
    } else if (event instanceof EndElement) {
        EndElement element = (EndElement) event;
        String localName = element.getName().getLocalPart();

        if ("testcase".equals(localName) && StringUtils.isNotEmpty(testName)) { // NON-NLS
            addItem(new TestResult(packageName, className, testName, status, duration, started));
        }
    }
}