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:edu.utah.further.core.api.xml.XmlUtil.java

/**
 * @param xmlReader/* w w  w. j  a  v a  2 s  . c o  m*/
 * @param index
 */
private static void printAttribute(final PrintStream os, final XMLStreamReader xmlReader, final int index) {
    final String prefix = xmlReader.getAttributePrefix(index);
    final String namespace = xmlReader.getAttributeNamespace(index);
    final String localName = xmlReader.getAttributeLocalName(index);
    final String value = xmlReader.getAttributeValue(index);
    os.print(Strings.SPACE_STRING);
    os.print(getName(prefix, namespace, localName));
    os.print("=" + quote(value));
}

From source file:net.di2e.ecdr.libs.result.relevance.TextParser.java

/**
 * Given xml as a string, this method will parse out element text and CDATA text. It separates
 * each by one space character.//from w  ww .ja  va2s .  c  o m
 *
 * @param xmlData
 *            XML as a {@code String}
 * @return parsed CDATA and element text
 */
protected static String parseTextFrom(String xmlData) {

    StringBuilder builder = new StringBuilder();

    XMLStreamReader xmlStreamReader;

    try {
        // xml parser does not handle leading whitespace
        xmlStreamReader = xmlInputFactory.createXMLStreamReader(new StringReader(xmlData));

        while (xmlStreamReader.hasNext()) {
            int event = xmlStreamReader.next();

            if (event == XMLStreamConstants.CHARACTERS || event == XMLStreamConstants.CDATA) {

                String text = xmlStreamReader.getText();

                if (StringUtils.isNotBlank(text)) {
                    builder.append(" " + text.trim());
                }

            }
            if (event == XMLStreamConstants.START_ELEMENT) {
                for (int i = 0; i < xmlStreamReader.getAttributeCount(); i++) {

                    String text = xmlStreamReader.getAttributeValue(i);

                    if (StringUtils.isNotBlank(text)) {
                        builder.append(" " + text.trim());
                    }
                }
            }
        }
    } catch (XMLStreamException e1) {
        LOGGER.warn("Failure occurred in parsing the xml data (" + xmlData
                + "). No data has been stored or indexed.", e1);
    }

    return builder.toString();
}

From source file:Main.java

private static void parseRestOfDocument(XMLStreamReader reader) throws XMLStreamException {

    while (reader.hasNext()) {
        int type = reader.next();
        switch (type) {
        case XMLStreamConstants.START_ELEMENT:
            System.out.println(reader.getLocalName());
            if (reader.getNamespaceURI() != null) {
                String prefix = reader.getPrefix();
                if (prefix == null) {
                    prefix = "[None]";
                }//ww  w  .  ja va2s  .c  om
                System.out.println("prefix = '" + prefix + "', URI = '" + reader.getNamespaceURI() + "'");
            }

            if (reader.getAttributeCount() > 0) {
                for (int i = 0; i < reader.getAttributeCount(); i++) {
                    System.out.println("Attribute (name = '" + reader.getAttributeLocalName(i) + "', value = '"
                            + reader.getAttributeValue(i) + "')");
                    String attURI = reader.getAttributeNamespace(i);
                    if (attURI != null) {
                        String attPrefix = reader.getAttributePrefix(i);
                        if (attPrefix == null || attPrefix.equals("")) {
                            attPrefix = "[None]";
                        }
                        System.out.println("prefix=" + attPrefix + ",URI=" + attURI);
                    }
                }
            }

            break;
        case XMLStreamConstants.END_ELEMENT:
            System.out.println("XMLStreamConstants.END_ELEMENT");
            break;
        case XMLStreamConstants.CHARACTERS:
            if (!reader.isWhiteSpace()) {
                System.out.println("CD:" + reader.getText());
            }
            break;
        case XMLStreamConstants.DTD:
            System.out.println("DTD:" + reader.getText());
            break;
        case XMLStreamConstants.SPACE:
            System.out.println(" ");
            break;
        case XMLStreamConstants.COMMENT:
            System.out.println(reader.getText());
            break;
        default:
            System.out.println(type);
        }
    }
}

From source file:MDRevealer.ResistanceTests.java

private static ArrayList<HashMap<String, String>> parse_test(File rt_input)
        throws XMLStreamException, FileNotFoundException {
    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLStreamReader xsr = xif.createXMLStreamReader(new FileInputStream(rt_input));
    ArrayList<HashMap<String, String>> test = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> hm = new HashMap<String, String>();
    String key = "null";
    while (xsr.hasNext() == true) {

        int constant = xsr.next();

        switch (constant) {
        case XMLStreamConstants.START_ELEMENT:
            key = xsr.getLocalName();//w  ww.  ja  v  a2  s .co  m
            if (xsr.getAttributeCount() > 0) {
                for (int i = 0; i < xsr.getAttributeCount(); i++) {
                    hm.put(xsr.getAttributeLocalName(i), xsr.getAttributeValue(i));
                }
                test.add(hm);
                hm = new HashMap<String, String>();
            }
            break;
        case XMLStreamConstants.CHARACTERS:
            if (!xsr.isWhiteSpace()) {
                hm.put(key, xsr.getText());
                test.add(hm);
            }
            hm = new HashMap<String, String>();
            break;
        case XMLStreamConstants.END_ELEMENT:
            if (xsr.getLocalName().equals("condition")) {
                hm.put("condition_over", "true");
                test.add(hm);
                hm = new HashMap<String, String>();
            }
            break;
        }
    }
    return test;
}

From source file:com.stepstone.sonar.plugin.coldfusion.cflint.xml.TagAttribute.java

protected Optional<String> getAttributeValue(String name, XMLStreamReader stream) {

    for (int i = 0; i < stream.getAttributeCount(); i++) {

        if (name.equalsIgnoreCase(stream.getAttributeLocalName(i))) {
            return Optional.of(StringUtils.trimToEmpty(stream.getAttributeValue(i)));
        }/*from   w  w w .  j  a v a  2 s. com*/
    }

    return Optional.absent();
}

From source file:XmlReaderToWriter.java

public static void write(XMLStreamReader xmlr, XMLStreamWriter writer) throws XMLStreamException {
    switch (xmlr.getEventType()) {
    case XMLEvent.START_ELEMENT:
        final String localName = xmlr.getLocalName();
        final String namespaceURI = xmlr.getNamespaceURI();
        if (namespaceURI != null && namespaceURI.length() > 0) {
            final String prefix = xmlr.getPrefix();
            if (prefix != null)
                writer.writeStartElement(prefix, localName, namespaceURI);
            else//from  ww  w .ja  v a 2s.c  om
                writer.writeStartElement(namespaceURI, localName);
        } else {
            writer.writeStartElement(localName);
        }

        for (int i = 0, len = xmlr.getNamespaceCount(); i < len; i++) {
            writer.writeNamespace(xmlr.getNamespacePrefix(i), xmlr.getNamespaceURI(i));
        }

        for (int i = 0, len = xmlr.getAttributeCount(); i < len; i++) {
            String attUri = xmlr.getAttributeNamespace(i);
            if (attUri != null)
                writer.writeAttribute(attUri, xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i));
            else
                writer.writeAttribute(xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i));
        }
        break;
    case XMLEvent.END_ELEMENT:
        writer.writeEndElement();
        break;
    case XMLEvent.SPACE:
    case XMLEvent.CHARACTERS:
        writer.writeCharacters(xmlr.getTextCharacters(), xmlr.getTextStart(), xmlr.getTextLength());
        break;
    case XMLEvent.PROCESSING_INSTRUCTION:
        writer.writeProcessingInstruction(xmlr.getPITarget(), xmlr.getPIData());
        break;
    case XMLEvent.CDATA:
        writer.writeCData(xmlr.getText());
        break;

    case XMLEvent.COMMENT:
        writer.writeComment(xmlr.getText());
        break;
    case XMLEvent.ENTITY_REFERENCE:
        writer.writeEntityRef(xmlr.getLocalName());
        break;
    case XMLEvent.START_DOCUMENT:
        String encoding = xmlr.getCharacterEncodingScheme();
        String version = xmlr.getVersion();

        if (encoding != null && version != null)
            writer.writeStartDocument(encoding, version);
        else if (version != null)
            writer.writeStartDocument(xmlr.getVersion());
        break;
    case XMLEvent.END_DOCUMENT:
        writer.writeEndDocument();
        break;
    case XMLEvent.DTD:
        writer.writeDTD(xmlr.getText());
        break;
    }
}

From source file:babel.content.pages.Page.java

public void unpersist(XMLStreamReader reader) throws XMLStreamException {
    String elemTag;// w w  w.j av  a2s  .co m
    PageVersion ver;

    m_pageURL = reader.getAttributeValue(0);
    m_versions.clear();

    while (true) {
        int event = reader.next();

        if (event == XMLStreamConstants.END_ELEMENT && XML_TAG_PAGE.equals(reader.getName().toString())) {
            break;
        }

        if (event == XMLStreamConstants.START_ELEMENT) {
            elemTag = reader.getName().toString();

            if ("MetaData".equals(elemTag)) {
                m_pageProps.unpersist(reader);
            } else if ("PageVersion".equals(elemTag)) {
                ver = new PageVersion();
                ver.unpersist(reader);

                m_versions.add(ver);
            }
        }
    }
}

From source file:XmlStreamUtils.java

public static String printEvent(XMLStreamReader xmlr) {
    StringBuffer b = new StringBuffer();
    b.append("EVENT:[" + xmlr.getLocation().getLineNumber() + "][" + xmlr.getLocation().getColumnNumber()
            + "] ");
    b.append(getName(xmlr.getEventType()));
    b.append(" [");
    switch (xmlr.getEventType()) {
    case XMLStreamReader.START_ELEMENT:
        b.append("<");
        printName(xmlr, b);// w  w  w.  j av a  2 s .c  o  m
        for (int i = 0; i < xmlr.getNamespaceCount(); i++) {
            b.append(" ");
            String n = xmlr.getNamespacePrefix(i);
            if ("xmlns".equals(n)) {
                b.append("xmlns=\"" + xmlr.getNamespaceURI(i) + "\"");
            } else {
                b.append("xmlns:" + n);
                b.append("=\"");
                b.append(xmlr.getNamespaceURI(i));
                b.append("\"");
            }
        }

        for (int i = 0; i < xmlr.getAttributeCount(); i++) {
            b.append(" ");
            printName(xmlr.getAttributePrefix(i), xmlr.getAttributeNamespace(i), xmlr.getAttributeLocalName(i),
                    b);
            b.append("=\"");
            b.append(xmlr.getAttributeValue(i));
            b.append("\"");
        }

        b.append(">");
        break;
    case XMLStreamReader.END_ELEMENT:
        b.append("</");
        printName(xmlr, b);
        for (int i = 0; i < xmlr.getNamespaceCount(); i++) {
            b.append(" ");
            String n = xmlr.getNamespacePrefix(i);
            if ("xmlns".equals(n)) {
                b.append("xmlns=\"" + xmlr.getNamespaceURI(i) + "\"");
            } else {
                b.append("xmlns:" + n);
                b.append("=\"");
                b.append(xmlr.getNamespaceURI(i));
                b.append("\"");
            }
        }
        b.append(">");
        break;
    case XMLStreamReader.SPACE:
    case XMLStreamReader.CHARACTERS:
        // b.append(xmlr.getText());
        int start = xmlr.getTextStart();
        int length = xmlr.getTextLength();
        b.append(new String(xmlr.getTextCharacters(), start, length));
        break;
    case XMLStreamReader.PROCESSING_INSTRUCTION:
        String target = xmlr.getPITarget();
        if (target == null)
            target = "";
        String data = xmlr.getPIData();
        if (data == null)
            data = "";
        b.append("<?");
        b.append(target + " " + data);
        b.append("?>");
        break;
    case XMLStreamReader.CDATA:
        b.append("<![CDATA[");
        if (xmlr.hasText())
            b.append(xmlr.getText());
        b.append("]]&gt;");
        break;

    case XMLStreamReader.COMMENT:
        b.append("<!--");
        if (xmlr.hasText())
            b.append(xmlr.getText());
        b.append("-->");
        break;
    case XMLStreamReader.ENTITY_REFERENCE:
        b.append(xmlr.getLocalName() + "=");
        if (xmlr.hasText())
            b.append("[" + xmlr.getText() + "]");
        break;
    case XMLStreamReader.START_DOCUMENT:
        b.append("<?xml");
        b.append(" version='" + xmlr.getVersion() + "'");
        b.append(" encoding='" + xmlr.getCharacterEncodingScheme() + "'");
        if (xmlr.isStandalone())
            b.append(" standalone='yes'");
        else
            b.append(" standalone='no'");
        b.append("?>");
        break;

    }
    b.append("]");
    return b.toString();
}

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 w w . ja v  a2s .  c  om
 * @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());
            }
        }
    }
}

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   ww w  .j ava 2s  .com
    }
    return relationship;
}