The path elements can include an index. The first path element must not have an index, and its name must match the name of the starting node. If the starting node is a Document, the root Element of the document is used as the starting point. Path elements must be separated by the slash character. If the path starts with a slash,the slash is ignored. If the element or attribute identified by the path is not present as a child of the starting node,the empty string is returned. If a path element identifies an attribute, any subsequent path elements are ignored.
public static String getTextContent(Node node, String path) {
if (node instanceof Document) node = ((Document)node).getDocumentElement();
if (!(node instanceof Element)) return "";
Element el = (Element)node;
path = path.replaceAll("\\s","");
if (path.startsWith("/")) path = path.substring(1);
String[] pathElements = path.split("/");
if (!pathElements[0].equals(el.getTagName())) return "";
for (int i=1; i
Comments
Post a Comment