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.
It's an easy bro, the short and quick answer is
to get the attribute name use this
to get the attribute value use this
the full code for your method (I eliminated the return type) and re-arranged the code
and here's a complete code
hope this is useful and solve your problem (:
you can also have a look for this simple tutorial java xml on jenkov