Java tutorial
//package com.java2s; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.Element; import org.w3c.dom.Attr; public class Main { /** * Get textvalue from element path or attribute * @param doc * @param path path ("/root/channel/name") or * attribute ("/root/channel/@id") * @return */ public static String getText(Document doc, String[] path) { NodeList nodes = doc.getElementsByTagName(path[0]); Element element = (Element) nodes.item(0); return getText(element, path, 1); } /** * Get textvalue from element path or attribute. * @param doc * @param path * @return */ public static String getText(Document doc, String path) { return getText(doc, toPathArray(path)); } /** * Get textvalue from path or attribute. This returns text from first * node found or NULL if path if unknown * @param element xml element to be used as a root node * @param path /elementA/elementB * /elementA/elementB/@attributeName * @return */ public static String getText(Element element, String path) { return getText(element, toPathArray(path)); } public static String getText(Element rootElem, String[] path) { NodeList nodes = rootElem.getElementsByTagName(path[0]); if (nodes == null || nodes.getLength() < 1) { // failsafe if first item is @attribute identifier // then read attribute value from rootElement. boolean isAttrText = path[0].charAt(0) == '@'; if (!isAttrText) return null; Attr attr = rootElem.getAttributeNode(path[0].substring(1)); return (attr != null ? attr.getValue() : null); } Element element = (Element) nodes.item(0); return getText(element, path, 1); } /** * Get textvalue from path or attribute. This is * called by other getText() methods. Endusers usually * should not call this directly. * @param element * @param path * @param pathOffset * @return */ public static String getText(Element element, String[] path, int pathOffset) { int len = path.length; boolean isAttrText = path[len - 1].charAt(0) == '@'; if (isAttrText) len--; // last item is @attribute identifier // start path from given offset index for (int i = pathOffset; i < len; i++) { if (element == null) return null; NodeList nodes = element.getElementsByTagName(path[i]); element = (Element) nodes.item(0); } if (isAttrText) { if (element == null) return null; Attr attr = element.getAttributeNode(path[len].substring(1)); return (attr != null ? attr.getValue() : null); } else { return getSimpleText(element); } } /** * Get text from element. * This concatenates all "#text" child nodes. * @param element * @return */ public static String getText(Element element) { return getSimpleText(element); } /** * Split string to array of path array. * @param path "/root/elementA/elementB" * @return */ public static String[] toPathArray(String path) { if (path.charAt(0) == '/') path = path.substring(1); return path.split("/"); } /** * Get textvalue from element. Loop all #text chidlnoes. * This is used by getText() methods, endusers usually * should not call this directly. */ private static String getSimpleText(Element element) { if (element == null) return null; StringBuilder sb = new StringBuilder(); NodeList nodes = element.getChildNodes(); Node node; for (int i = 0; i < nodes.getLength(); i++) { node = nodes.item(i); if (node.getNodeType() == Node.TEXT_NODE) sb.append(node.getNodeValue()); } return sb.toString().trim(); } }