Java tutorial
//package com.java2s; import java.util.HashMap; import java.util.Map; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; public class Main { /** * This is an ugly hack, there must be a nicer way to do it. * Using getPrefix doesn't work because it stops us from getting * xmlns: attributes, which is what I'm using this for. Using * getNamespace doesn't seem to work on xmlns attributes either. * Trims the prefix on the map key. * @param element the element from which to retrieve the attributes. * @param prefix the prefix of the attributes to retrieve * @return a Map containing the attributes names and their values. */ public static Map getAttributesWithPrefix(Element element, String prefix) { Map result = new HashMap(); prefix += ":"; NamedNodeMap attributes = element.getAttributes(); if (attributes == null) return result; for (int i = 0; i != attributes.getLength(); i++) { Node attribute = attributes.item(i); if (attribute.getNodeName().startsWith(prefix)) { result.put(attribute.getNodeName().substring(prefix.length()), attribute.getNodeValue()); } } return result; } }