XMLEventReader Stax API not able to getName and value for Attribute in JDK 1.8

2.1k views Asked by At

I am using Stax XML EventReader for reading from xml. I have to verify a few tags in xml for which i am using the same. I am able to successfully read the tagname and characters from the xml but unable to read the attribute name and value. I am using jdk 1.8.111

XML:

<xml>
<status request_id="fa844c52-daeb-4d24-920b-581ce2ac1afe1482232642212"     response_time="00:00:00:039">

CODE:

public static String XMLParseAttribute() throws XMLStreamException, IOException {

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    in = IOUtils.toInputStream(URLResponse, "UTF-8");
    eventReader = inputFactory.createXMLEventReader(in);                
    XMLEvent event = eventReader.nextEvent();

        while(eventReader.hasNext())
        {
            XMLEvent event = eventReader.nextEvent();

                if (event.isStartElement()) {
                    Iterator<Attribute> itr = event.asStartElement().getAttributes();
                    while(itr.hasNext()){
                        Attribute attribute = itr.next();
                        attribute. //get name and value here
                    }
                }
             }
        //Something like this below
        return attribute.getName().toString();
        }

Kindly guide me as to how to use this XMLEventReader to read the attribute name and value.

1

There are 1 answers

2
Anas On BEST ANSWER

It's an easy bro, the short and quick answer is

to get the attribute name use this

String name = attribute.getName().toString();

to get the attribute value use this

String value = attribute.getValue();

the full code for your method (I eliminated the return type) and re-arranged the code

public static void XMLParseAttribute() throws XMLStreamException, IOException
{

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    InputStream in = new FileInputStream("input.xml");
    XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
    XMLEvent event;

    while (eventReader.hasNext())
    {
        event = eventReader.nextEvent();

        if (event.isStartElement())
        {
            String elemntName = event.asStartElement().getName().getLocalPart();
            System.out.println(elemntName);
            Iterator<Attribute> iterator = event.asStartElement().getAttributes();
            while (iterator.hasNext())
            {
                Attribute attribute = iterator.next();
                String value = attribute.getValue();
                String name = attribute.getName().toString();
                System.out.println("\t" + name + " " + value);
            }
        }
    }
}

and here's a complete code

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import javax.xml.stream.*;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.XMLEvent;

public class XmlReader
{

public static void main(String[] args) throws XMLStreamException, IOException
{
    XMLParseAttribute("input.xml");
}

public static void XMLParseAttribute(String fileName) throws XMLStreamException, IOException
{

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    InputStream in = new FileInputStream(fileName);
    XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
    XMLEvent event;

    while (eventReader.hasNext())
    {
        event = eventReader.nextEvent();

        if (event.isStartElement())
        {
            String elemntName = event.asStartElement().getName().getLocalPart();
            System.out.println(elemntName);
            Iterator<Attribute> iterator = event.asStartElement().getAttributes();
            while (iterator.hasNext())
            {
                Attribute attribute = iterator.next();
                String value = attribute.getValue();
                String name = attribute.getName().toString();
                System.out.println("\t" + name + " " + value);
            }
        }
    }
}}

hope this is useful and solve your problem (:
you can also have a look for this simple tutorial java xml on jenkov