Java examples for XML:XML Attribute
get Attribute In XML
//package com.java2s; import org.w3c.dom.Element; public class Main { public static String getAttributeInXML(org.w3c.dom.Document doc, String tagName, String attributeName) { String output = ""; org.w3c.dom.NodeList ontologyTypes = doc .getElementsByTagName(tagName); output = ((Element) ontologyTypes.item(0)) .getAttribute(attributeName); return output; }/*w w w . ja va 2 s . c om*/ public static String getAttributeInXML(org.w3c.dom.Node node, String tagName, String attributeName) { String output = ""; org.w3c.dom.NodeList nodes = node.getChildNodes(); if (!attributeName.equals("_content")) for (int i = 0; i < nodes.getLength(); i++) { if (nodes.item(i).getNodeName().equals(tagName)) { output = ((Element) nodes.item(i)) .getAttribute(attributeName); } } else for (int i = 0; i < nodes.getLength(); i++) { if (nodes.item(i).getNodeName().equals(tagName)) { output = (nodes.item(i)).getTextContent(); } } return output; } }