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(String namespaceURI, String localName);

Source Link

Document

Returns the normalized attribute value of the attribute with the namespace and localName If the namespaceURI is null the namespace is not checked for equality

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext context = JAXBContext.newInstance(User.class);

    XMLInputFactory xif = XMLInputFactory.newInstance();
    FileInputStream fis = new FileInputStream("input.xml");
    XMLStreamReader xsr = xif.createXMLStreamReader(fis);
    xsr.nextTag();/*  w w  w .ja v a 2 s.  c om*/
    String noNamespaceSchemaLocation = xsr.getAttributeValue(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI,
            "noNamespaceSchemaLocation");
    System.out.println(noNamespaceSchemaLocation);

    Unmarshaller um = context.createUnmarshaller();
    User response = (User) um.unmarshal(xsr);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLInputFactory xif = XMLInputFactory.newFactory();
    XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("data.xml"));
    xsr.nextTag(); // advance to Employees tag
    xsr.nextTag(); // advance to first Employer element
    Map<String, String> map = new HashMap<String, String>();
    while (xsr.getLocalName().equals("Employee")) {
        map.put(xsr.getAttributeValue("", "id"), xsr.getElementText());
        xsr.nextTag(); // advance to next Employer element
    }//from  ww  w  .  j  ava  2 s.c  o m
}

From source file:StAXTest.java

public static void main(String[] args) throws Exception {
    String urlString;/*from  w  w  w . jav  a2  s  .  co m*/
    if (args.length == 0) {
        urlString = "http://www.w3c.org";
        System.out.println("Using " + urlString);
    } else
        urlString = args[0];
    URL url = new URL(urlString);
    InputStream in = url.openStream();
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader parser = factory.createXMLStreamReader(in);
    while (parser.hasNext()) {
        int event = parser.next();
        if (event == XMLStreamConstants.START_ELEMENT) {
            if (parser.getLocalName().equals("a")) {
                String href = parser.getAttributeValue(null, "href");
                if (href != null)
                    System.out.println(href);
            }
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml"));
    xsr.nextTag(); // Advance to statements element

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    while (xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
        File file = new File("out/" + xsr.getAttributeValue(null, "account") + ".xml");
        t.transform(new StAXSource(xsr), new StreamResult(file));
    }//  ww  w  .  j a v  a  2 s  . c  om
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml"));
    xsr.nextTag(); // Advance to statements element

    while (xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        File file = new File("out" + xsr.getAttributeValue(null, "account") + ".xml");
        t.transform(new StAXSource(xsr), new StreamResult(file));
    }// w ww .  j  a  v  a  2  s  .  c om
}

From source file:Main.java

public static float readFloatAttribute(XMLStreamReader reader, String attributeName) {
    return Float.valueOf(reader.getAttributeValue(null, attributeName));
}

From source file:Main.java

/**
 * Returns the value of the attribute with the given name.
 * //from  w w w.  j a  v a  2  s. c  o  m
 * @param reader The xml stream reader
 * @param name The name of the attribute.
 * @return The value of the attribute, or <code>null</code> if the
 *         attribute wasn't present.
 */
public static final String attributeValue(XMLStreamReader reader, QName name) {

    return reader.getAttributeValue(name.getNamespaceURI(), name.getLocalPart());

}

From source file:Main.java

/**
 * Returns the value of the attribute with the given non-qualified name.
 * //from ww w .java  2 s .co m
 * @param reader The xml stream reader
 * @param name The name of the attribute.
 * @return The value of the unqualified attribute, or <code>null</code>
 *         if the attribute wasn't present.
 */
public static final String attributeValue(XMLStreamReader reader, String name) {

    return reader.getAttributeValue("", name);

}

From source file:Main.java

public static int readIntAttribute(XMLStreamReader reader, String attributeName) {
    return Integer.valueOf(reader.getAttributeValue(null, attributeName));
}

From source file:Main.java

public static boolean readBoolAttribute(XMLStreamReader reader, String attributeName) {
    return Boolean.valueOf(reader.getAttributeValue(null, attributeName));
}