Java examples for XML:XML Attribute
extract Attributes From Xml Tag Into Map
//package com.java2s; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.events.Attribute; import javax.xml.stream.events.StartElement; import javax.xml.stream.events.XMLEvent; import java.io.InputStream; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Main { public static Map<String, String> extractAttributesFromXmlTagIntoMap( InputStream input, String tagName) throws XMLStreamException { XMLEventReader eventReader = XMLInputFactory.newFactory() .createXMLEventReader(input); final Map<String, String> attributesMap = new HashMap<String, String>(); while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); if (event.isStartElement()) { final StartElement startElement = event.asStartElement(); if (startElement.getName().getLocalPart() == tagName) { Iterator<Attribute> attributes = startElement .getAttributes(); while (attributes.hasNext()) { final Attribute attr = attributes.next(); attributesMap.put(attr.getName().toString(), attr .getValue().toString()); }/*from w w w.j a va2 s. com*/ } } } return attributesMap; } }