List of usage examples for org.w3c.dom Element getChildNodes
public NodeList getChildNodes();
NodeList
that contains all children of this node. From source file:Main.java
public static List<Element> findElementsByAttribute(Element node, String tagName, String attrName, List<String> attrValues) { List<Element> result = new LinkedList<Element>(); NodeList nodeList = node.getChildNodes(); if (nodeList == null) { return result; }/* www.jav a 2 s .c o m*/ for (int i = 0; i < nodeList.getLength(); ++i) { Element element = checkIfElement(nodeList.item(i), tagName); if (element != null) { for (String value : attrValues) { if (element.getAttribute(attrName).equals(value)) { result.add(element); break; } } } } return result; }
From source file:Main.java
public static List<Element> elements(final Element element, final String tagName) { List<Element> elements = null; final NodeList nodeList = element.getChildNodes(); if (nodeList != null) { for (int i = 0; i < nodeList.getLength(); i++) { final Node child = nodeList.item(i); if (Element.class.isAssignableFrom(child.getClass())) { final Element childElement = (Element) child; final String childTagName = getTagLocalName(childElement); if (childTagName.equals(tagName)) { if (elements == null) { elements = new ArrayList<Element>(); }// w w w . j av a 2 s . co m elements.add(childElement); } } } } return elements; }
From source file:Main.java
private static void addAllElementsWithAttrId(final List<Element> list, final Element element, final String namespace) { if (elementHasId(element, namespace)) { list.add(element);/* w w w . j a v a 2 s .c o m*/ } NodeList children = element.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child instanceof Element == false) { continue; } Element childElement = (Element) child; if (elementHasId(childElement, namespace)) { list.add(childElement); } addAllElementsWithAttrId(list, childElement, namespace); } }
From source file:Main.java
/** * Method getChildTags. Get all direct children of given element which * match the given tag./*from w w w .j a v a 2 s . c o m*/ * This method only looks at the direct children of the given node, and * doesn't descent deeper into the tree. If a '*' is passed as tag, * all elements are returned. * * @param el Element where to get children from * @param tag Tag to match. Use '*' to match all tags. * @return Collection Collection containing all elements found. If * size() returns 0, then no matching elements * were found. All items in the collection can * be safely cast to type * <code>org.w3c.dom.Element</code>. */ public static Collection<Node> getChildTags(Element el, String tag) { Collection<Node> c; NodeList nl; int len; boolean allChildren; c = new LinkedList<Node>(); nl = el.getChildNodes(); len = nl.getLength(); if ("*".equals(tag)) { allChildren = true; } else { allChildren = false; } for (int i = 0; i < len; i++) { Node n = nl.item(i); if (n instanceof Element) { Element e = (Element) n; if (allChildren || e.getTagName().equals(tag)) { c.add(n); } } } return c; }
From source file:Main.java
/** * Method getFirstChildTag. Return the first child-node which is an element * with tagName equal to given tag./*from www.j av a2 s.c om*/ * This method only looks at the direct children of the given node, and * doesn't descent deeper into the tree. * * @param el Element where to get children from * @param tag Tag to match * @return Element The element found, or <code>null</code> if no match * found. */ static public Element getFirstChildTag(Element el, String tag) { NodeList nl; int len; nl = el.getChildNodes(); len = nl.getLength(); for (int i = 0; i < len; ++i) { Node n = nl.item(i); if (n instanceof Element) { Element elem = (Element) n; if (elem.getTagName().equals(tag)) { return elem; } } } return null; }
From source file:Main.java
/** * Access an immediate child element inside the given Element * * @param element the starting element, cannot be null. * @param namespaceURI name space of the child element to look for, * cannot be null. Use "*" to match all namespaces. * @param elemName the name of the child element to look for, cannot be * null./*from w w w. j a v a2 s .com*/ * @return the first immediate child element inside element, or * <code>null</code> if the child element is not found. */ public static Element getChildElement(Element element, String namespaceURI, String elemName) { NodeList list = element.getChildNodes(); int len = list.getLength(); for (int i = 0; i < len; i++) { Node n = list.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { if (elemName.equals(n.getLocalName()) && ("*".equals(namespaceURI) || namespaceURI.equals(n.getNamespaceURI()))) { return (Element) n; } } } return null; }
From source file:Main.java
private static Element getChildElement(Document document, Element origElement, String subElementName) { NodeList buildNodes = origElement.getChildNodes(); int numChildrenOfOrig = buildNodes.getLength(); for (int indexChildOfOrig = 0; indexChildOfOrig < numChildrenOfOrig; indexChildOfOrig++) { Node child = buildNodes.item(indexChildOfOrig); if (child.getNodeName().equals(subElementName)) { return (Element) child; }// w ww. j a v a 2s . c o m } //if we are here, the child node was never found, so create it now Element newElem = document.createElement(subElementName); origElement.appendChild(newElem); return newElem; }
From source file:Main.java
public static List<Element> getElementsByTagNames(Element element, String[] tagNames) { List<Element> children = new ArrayList<Element>(); if (element != null && tagNames != null) { List tagList = Arrays.asList(tagNames); NodeList nodes = element.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node child = nodes.item(i); if (child.getNodeType() == Node.ELEMENT_NODE && tagList.contains(((Element) child).getTagName())) { children.add((Element) child); }//from w ww . j a v a 2 s.c o m } } return children; }
From source file:Main.java
public static java.util.List<org.w3c.dom.Element> getChildElementsByTagName(org.w3c.dom.Element xmlParent, String tagName) {/*from w ww . ja v a2 s. c om*/ java.util.List<org.w3c.dom.Element> rv = new java.util.LinkedList<org.w3c.dom.Element>(); org.w3c.dom.NodeList nodeList = xmlParent.getChildNodes(); final int N = nodeList.getLength(); for (int i = 0; i < N; i++) { org.w3c.dom.Node node = nodeList.item(i); if (node instanceof org.w3c.dom.Element) { org.w3c.dom.Element element = (org.w3c.dom.Element) node; if (tagName.equals(element.getTagName())) { rv.add(element); } } } return rv; }
From source file:Main.java
public static List<Element> elements(final Element element) { List<Element> elements = null; if (element != null) { final NodeList nodeList = element.getChildNodes(); if (nodeList != null && nodeList.getLength() > 0) { elements = new ArrayList<Element>(); for (int i = 0; i < nodeList.getLength(); i++) { final Node node = nodeList.item(i); if (node instanceof Element) { elements.add((Element) node); }//ww w .ja v a 2 s . c om } } } return elements; }