List of utility methods to do XML Element Child Get
String | getFullTextChildrenFromElement(Element element) Method getFullTextChildrenFromElement StringBuilder sb = new StringBuilder(); Node child = element.getFirstChild(); while (child != null) { if (child.getNodeType() == Node.TEXT_NODE) { sb.append(((Text) child).getData()); child = child.getNextSibling(); return sb.toString(); |
void | printChildElements(Element root, PrintStream out, boolean recurse, String prefix) Whenever you'd need to print a configuration node and/or its children. out.print(prefix + "<" + root.getNodeName()); NamedNodeMap attrs = root.getAttributes(); Node node; for (int i = 0; i < attrs.getLength(); i++) { node = attrs.item(i); out.print(" " + node.getNodeName() + "=\"" + node.getNodeValue() + "\""); out.println(">"); String data = getText(root); if (data != null && data.trim().length() > 0) out.println(prefix + "\t" + data); data = getCData(root); if (data != null && data.trim().length() > 0) out.println(prefix + "\t<![CDATA[" + data + "]]>"); NodeList nodes = root.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { node = nodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { if (recurse) printChildElements((Element) node, out, recurse, prefix + "\t"); else out.println(prefix + node.getNodeName()); out.println(prefix + "</" + root.getNodeName() + ">"); |
String | valueOfFirstChildWithName(Element element, String name) value Of First Child With Name Element e = firstChildWithName(element, name); if (e != null) { Node firstChild = e.getFirstChild(); if (firstChild != null) return firstChild.getNodeValue(); return null; |
String | attributeOfFirstChildWithName(Element element, String name, String attribute) attribute Of First Child With Name Element e = firstChildWithName(element, name); if (element != null) { return e.getAttribute(attribute); return null; |
List | childrenWithName(Element e, String name) children With Name final List<Element> ret = new ArrayList<Element>(); NodeList list = e.getChildNodes(); final int length = list.getLength(); for (int i = 0; i < length; ++i) { Node n = list.item(i); if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) ret.add((Element) n); ... |
CDATASection | getCDataNode(Element element) Returns element's CDATA child node (if it has one). return (CDATASection) getChildByType(element,
Node.CDATA_SECTION_NODE);
|
List | locateElements(Element root, String tagName, String keyAttributeName, String keyAttributeValue) Looks through all child elements of the specified root (recursively) and returns the elements that corresponds to all parameters. List<Element> result = new ArrayList<Element>(); NodeList nodes = root.getChildNodes(); Node node; int len = nodes.getLength(); for (int i = 0; i < len; i++) { node = nodes.item(i); if (node.getNodeType() != Node.ELEMENT_NODE) continue; ... |
Element | locateElement(Element root, String tagName, String keyAttributeName, String keyAttributeValue) Looks through all child elements of the specified root (recursively) and returns the first element that corresponds to all parameters. NodeList nodes = root.getChildNodes(); int len = nodes.getLength(); for (int i = 0; i < len; i++) { Node node = nodes.item(i); if (node.getNodeType() != Node.ELEMENT_NODE) continue; Element element = (Element) node; if (node.getNodeName().equals(tagName)) { ... |
String | valueOfFirstDescendantWithName(Element element, String name) value Of First Descendant With Name Element e = firstDescendantWithName(element, name); if (e != null) { return e.getFirstChild().getNodeValue(); return null; |
Element | firstDescendantWithName(Element element, String name) first Descendant With Name NodeList list = element.getElementsByTagName(name); if (list.getLength() > 0) { Node n = list.item(0); if (n.getNodeType() == Node.ELEMENT_NODE) return (Element) n; return null; |