Retrieves ElementValue
? for a given XML file. Here's a method that does just that:
@param
public static String getElementValue(String elementName) {
DocumentBuilderFactory dbf = null;
DocumentBuilder db = null;
Document doc = null;
try {
dbf = DocumentBuilderFactory.newInstance();
db = dbf.newDocumentBuilder();
doc = db.parse("foo.xml");
NodeList nodelist = doc.getElementsByTagName(elementName);
if (null != nodelist) {
Element elem = (Element)nodelist.item(0);
if (null != elem) {
Node namechild = elem.getFirstChild();
if (null != namechild)
return namechild.getNodeValue();
}
}
} catch (Exception e) {
}
return "Not Found";
}
XmlFaq CategoryCodeSamples