List of utility methods to do XML First Child Element
List | getFirstLevelChildElementsByTagName(Element parent, String elementName) Get all the first level child elements with a given tag name. NodeList nodeList = parent.getChildNodes(); ArrayList<Element> childList = new ArrayList<Element>(nodeList.getLength()); Node childNode = parent.getFirstChild(); while (childNode != null) { if (childNode.getNodeType() == Node.ELEMENT_NODE) { String localName = ((Element) childNode).getLocalName(); if (localName.equals(elementName)) { childList.add((Element) childNode); ... |
List | getFirstLevelChildElementsByTagName(Element parent, String elementName) get First Level Child Elements By Tag Name NodeList nodeList = parent.getChildNodes(); ArrayList<Element> childList = new ArrayList<Element>(nodeList.getLength()); Node childNode = parent.getFirstChild(); while (childNode != null) { if (childNode.getNodeType() == Node.ELEMENT_NODE) { String localName = ((Element) childNode).getLocalName(); if (localName.equals(elementName)) { childList.add((Element) childNode); ... |
String | getFirstMatchedValueByChildTagName(Node parent, String name) get First Matched Value By Child Tag Name for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() == Node.ELEMENT_NODE) { if (child.getNodeName().indexOf(name) != -1) { return child.getTextContent(); } else { String value = getFirstMatchedValueByChildTagName((Element) child, name); if (value != null) { return value; ... |
Element | getFirstMatchingDeepChildByTagName(final Element e, final String tagName) get First Matching Deep Child By Tag Name NodeList matchingElements = e.getElementsByTagName(tagName); if (matchingElements != null) { return (Element) matchingElements.item(0); } else { return null; |
Node | getFirstNamedChild(Node n, String name) Check if the given node has a child with the specified name Return the child of the specified name, or null if not found NodeList children = n.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node currentChild = children.item(i); String localName = currentChild.getNodeName(); if (localName != null && localName.equalsIgnoreCase(name)) { return currentChild; return null; |
Element | getFirstNamedChild(Node node, String name) Get the first child element with a specified name. if (node == null) return null; if (node instanceof Document) node = ((Document) node).getDocumentElement(); if (!(node instanceof Element)) return null; Node child = node.getFirstChild(); while (child != null) { ... |
Node | getFirstNamedChildNode(Element element, String string) get First Named Child Node NodeList children = element.getChildNodes(); if (children != null && children.getLength() > 0) { for (int i = 0; i < children.getLength(); i++) { Node item = children.item(i); if (item.getNodeName().equals(string)) { return item; return null; |
Node | getFirstNamedChildNode(Node root, String nodeName) get First Named Child Node Node current = root.getFirstChild(); while (current != null) { if (current.getNodeName().equalsIgnoreCase(nodeName)) { return current; current = current.getNextSibling(); return null; ... |
Element | getFirstNontextChild(Node d) get First Nontext Child Node n = d.getFirstChild(); while ((n != null) && (n.getNodeType() != Node.ELEMENT_NODE)) { n = n.getNextSibling(); return (Element) n; |
Node | getFirstNonTextChild(Node root) Returns the first non-text child node of the given node. Node resultNode = null; NodeList nl = root.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { if (!isTextNode(nl.item(i))) { resultNode = nl.item(i); break; return resultNode; |