Example usage for javax.xml.stream.events StartElement getAttributeByName

List of usage examples for javax.xml.stream.events StartElement getAttributeByName

Introduction

In this page you can find the example usage for javax.xml.stream.events StartElement getAttributeByName.

Prototype

public Attribute getAttributeByName(QName name);

Source Link

Document

Returns the attribute referred to by the qname.

Usage

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());
    }// w  ww  .  j  ava2  s.  c  o m
    return parent;
}

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: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());
    }/*from   w  w  w. j  a  v  a2s.c o m*/
    return attributeByName.getValue();
}

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  w w.  ja va 2s  .  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:com.predic8.membrane.core.interceptor.rest.XML2HTTP.java

/**
 * @return the attribute's value//from   ww w  .j  a va 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.hazelcast.simulator.probes.probes.ProbesResultXmlReader.java

private static void parseProbe(XMLEventReader reader, StartElement startElement, Map<String, Result> result)
        throws XMLStreamException {
    String name = startElement.getAttributeByName(new QName(PROBE_NAME.getName())).getValue();
    String type = startElement.getAttributeByName(new QName(PROBE_TYPE.getName())).getValue();

    Result probeResult = null;//from w  w  w.ja  v a 2s.co  m
    if (OperationsPerSecResult.XML_TYPE.equals(type)) {
        probeResult = parseOperationsPerSecResult(reader);
    } else if (MaxLatencyResult.XML_TYPE.equals(type)) {
        probeResult = parseMaxLatencyResult(reader);
    } else if (HdrLatencyDistributionResult.XML_TYPE.equals(type)) {
        probeResult = parseHdrLatencyProbeResult(reader);
    } else if (LatencyDistributionResult.XML_TYPE.equals(type)) {
        probeResult = parseLatencyDistributionResult(reader);
    }
    result.put(name, probeResult);
}

From source file:com.hazelcast.simulator.probes.probes.ProbesResultXmlReader.java

private static void parseBucket(XMLEventReader reader, StartElement element, LinearHistogram histogram)
        throws XMLStreamException {
    String upperBound = element.getAttributeByName(new QName(LATENCY_DIST_UPPER_BOUND.getName())).getValue();
    String values = element.getAttributeByName(new QName(LATENCY_DIST_VALUES.getName())).getValue();
    histogram.addMultipleValues(Integer.parseInt(upperBound) - 1, Integer.parseInt(values));

    while (reader.hasNext()) {
        XMLEvent xmlEvent = reader.nextEvent();
        if (xmlEvent.isEndElement()) {
            EndElement endElement = xmlEvent.asEndElement();
            if (LATENCY_DIST_BUCKET.getName().equals(endElement.getName().getLocalPart())) {
                return;
            }//from   w w  w  .java  2  s. c  o m
        }
    }
    throw new XMLStreamException("Unexpected end of the document");
}

From source file:gdv.xport.satz.xml.FeldReferenz.java

/**
 * Instantiiert eine Objekt mit den Werten, die ueber den uebergebenen
 * Parser gelesen werden.//from w  ww  . j a v  a  2s .  com
 *
 * @param parser the parser
 * @param element das Start-Element <feldreferenz referenz=... >
 * @throws XMLStreamException the XML stream exception
 */
public FeldReferenz(final XMLEventReader parser, final StartElement element) throws XMLStreamException {
    id = element.getAttributeByName(new QName("referenz")).getValue();
    Properties props = XmlHelper.parseSimpleElements(element.getName(), parser);
    this.bezeichner = new Bezeichner(props);
    this.bemerkung = props.getProperty("bemerkung", "");
    this.auspraegung = props.getProperty("auspraegung", "");
    LOG.debug("{} created.", this);
}

From source file:org.fcrepo.serialization.JcrXmlSerializer.java

private void validateJCRXML(final File file) throws InvalidSerializationFormatException, IOException {
    int depth = 0;
    try (final FileInputStream fis = new FileInputStream(file)) {
        final XMLEventReader reader = XMLInputFactory.newFactory().createXMLEventReader(fis);
        while (reader.hasNext()) {
            final XMLEvent event = reader.nextEvent();
            if (event.isStartElement()) {
                depth++;/* w  w  w .  j  a va2 s .  c  om*/
                final StartElement startElement = event.asStartElement();
                final Attribute nameAttribute = startElement
                        .getAttributeByName(new QName("http://www.jcp.org/jcr/sv/1.0", "name"));
                if (depth == 1 && nameAttribute != null && "jcr:content".equals(nameAttribute.getValue())) {
                    throw new InvalidSerializationFormatException(
                            "Cannot import JCR/XML starting with content node.");
                }
                if (depth == 1 && nameAttribute != null && "jcr:frozenNode".equals(nameAttribute.getValue())) {
                    throw new InvalidSerializationFormatException("Cannot import historic versions.");
                }
                final QName name = startElement.getName();
                if (!(name.getNamespaceURI().equals("http://www.jcp.org/jcr/sv/1.0")
                        && (name.getLocalPart().equals("node") || name.getLocalPart().equals("property")
                                || name.getLocalPart().equals("value")))) {
                    throw new InvalidSerializationFormatException(
                            "Unrecognized element \"" + name.toString() + "\", in import XML.");
                }
            } else {
                if (event.isEndElement()) {
                    depth--;
                }
            }
        }
        reader.close();
    } catch (XMLStreamException e) {
        throw new InvalidSerializationFormatException(
                "Unable to parse XML" + (e.getMessage() != null ? " (" + e.getMessage() + ")." : "."));
    }
}

From source file:com.hp.application.automation.tools.octane.tests.TestResultIterator.java

@Override
public boolean hasNext() {
    try {//from ww  w  .  ja va2s.c  o  m
        while (items.isEmpty() && !closed) {
            if (reader.hasNext()) {
                XMLEvent event = reader.nextEvent();
                if (event instanceof StartElement) {
                    StartElement element = (StartElement) event;
                    String localName = element.getName().getLocalPart();
                    if ("test_run".equals(localName)) {
                        String moduleName = element.getAttributeByName(new QName("module")).getValue();
                        String packageName = element.getAttributeByName(new QName("package")).getValue();
                        String className = element.getAttributeByName(new QName("class")).getValue();
                        String testName = element.getAttributeByName(new QName("name")).getValue();
                        long duration = Long
                                .valueOf(element.getAttributeByName(new QName("duration")).getValue());
                        TestResultStatus status = TestResultStatus
                                .fromPrettyName(element.getAttributeByName(new QName("status")).getValue());
                        long started = Long
                                .valueOf(element.getAttributeByName(new QName("started")).getValue());
                        items.add(new JUnitTestResult(moduleName, packageName, className, testName, status,
                                duration, started, null, null));
                    } else if ("build".equals(localName)) {
                        serverId = element.getAttributeByName(new QName("server_id")).getValue();
                        jobId = element.getAttributeByName(new QName("job_id")).getValue();
                        buildId = element.getAttributeByName(new QName("build_id")).getValue();
                        Attribute subType = element.getAttributeByName(new QName("sub_type"));
                        if (subType != null) {
                            this.subType = subType.getValue();
                        }
                    }
                }
            } else {
                closed = true;
                IOUtils.closeQuietly(input);
                reader.close();
            }
        }
        return !items.isEmpty();
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }
}