Java XML Attribute from Element getElementValues(final String elementName, final String attributeValue, final InputStream is)

Here you can find the source of getElementValues(final String elementName, final String attributeValue, final InputStream is)

Description

get Element Values

License

Open Source License

Parameter

Parameter Description
elementName a parameter
attributeValue a parameter
is a parameter

Exception

Parameter Description
XMLStreamException an exception
FactoryConfigurationError an exception
UnsupportedEncodingException an exception

Return

Collection

Declaration

public static Collection<String> getElementValues(final String elementName, final String attributeValue,
        final InputStream is)
        throws XMLStreamException, UnsupportedEncodingException, FactoryConfigurationError 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Manchester Centre for Integrative Systems Biology
 * University of Manchester/* ww w.  j a v  a  2 s  .  c o  m*/
 * Manchester M1 7ND
 * United Kingdom
 * 
 * Copyright (C) 2007 University of Manchester
 * 
 * This program is released under the Academic Free License ("AFL") v3.0.
 * (http://www.opensource.org/licenses/academic.php)
 *******************************************************************************/

import java.io.*;
import java.nio.charset.*;
import java.util.*;

import javax.xml.stream.*;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.events.*;

public class Main {
    /**
     * 
     * @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;
    }
}

Related

  1. getElements(Element root, String tagName, String attrName)
  2. getElementsByTagAndAttr(Element parent, String elemName, String attrName, String attrVal)
  3. getElementStringValue(Element element, String attribute)
  4. getElementStringValue(Element element, String attribute)
  5. getElementTextByAttr(Element modsroot, String nodename, String attrname, String attrvalue)
  6. getFirstAttribute(Element elem, String name, String attrName)
  7. getFloatAttribute(Element element, String name)
  8. getFloatAttribute(String name, Element el)
  9. getHeadAttr(Element annotU, String attrName)