List of usage examples for org.w3c.dom Node getChildNodes
public NodeList getChildNodes();
NodeList
that contains all children of this node. From source file:Main.java
public static Element getChildByAttrName(Node node, String attrName, String attrValue) { NodeList nodeList = node.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node n = nodeList.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { Element el = (Element) n; if (attrValue.equals(el.getAttribute(attrName))) { return el; }//from w w w.j a v a 2 s. c o m } } return null; }
From source file:Main.java
public static void removeNodesByName(Node node, String name) { NodeList nl = node.getChildNodes(); int i = 0;/* w ww . jav a2s. c o m*/ while (i < nl.getLength()) if (nl.item(i).getNodeName().equals(name)) node.removeChild(nl.item(i)); else i++; }
From source file:Main.java
private static Node ifNodeExists(Node upper, String path) { NodeList list = upper.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { if (list.item(i).getNodeName().equals(path)) return list.item(i); }// ww w . j ava 2 s . c om return null; }
From source file:Main.java
/** * This method takes Node object as argument and return the value of the * first child of the node.//from w w w . j av a 2s . c om * <p/> * If there is no such node, this will return null. * * @param node * Node object * @return <code>String</code> returns the value of the node */ public static String getTagValue(Node node) { NodeList childNodeList = node.getChildNodes(); String value; if (childNodeList == null) { value = ""; } else { StringBuffer buffer = new StringBuffer(); for (int j = 0; j < childNodeList.getLength(); j++) { buffer.append(childNodeList.item(j).getNodeValue()); } value = buffer.toString(); } return value; }
From source file:Main.java
/** * Extract nested text from a node. Currently does not handle coalescing * text nodes, CDATA sections, etc./*from w ww . j av a2 s . co m*/ * * @param parent a parent element * @return the nested text, or null if none was found * * @since 8.4 */ public static String findText(Node parent) { NodeList l = parent.getChildNodes(); for (int i = 0; i < l.getLength(); i++) { if (l.item(i).getNodeType() == Node.TEXT_NODE) { Text text = (Text) l.item(i); return text.getNodeValue(); } } return null; }
From source file:Main.java
/** * Checks if a node has a child of ELEMENT type. * @param node a node/*from w ww .j av a2 s . c om*/ * @return true if the node has a child of ELEMENT type */ public static boolean hasElementChild(Node node) { NodeList nl = node.getChildNodes(); Node child = null; int length = nl.getLength(); for (int i = 0; i < length; i++) { child = nl.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { return true; } } return false; }
From source file:Main.java
/** * Search a child node by type/* w w w. ja v a 2s . c o m*/ * * @param parent the parent node * @param nodeType the node type for searching * @return Node with the specified name * @see Node * @throws Exception */ public static Node findChildNodeByType(Node parent, String nodeType) throws Exception { NodeList nl = parent.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); if (node.getNodeName().equals("Node")) { String strType = node.getAttributes().getNamedItem("type").getNodeValue(); if (strType.equals(nodeType)) return node; } } return null; }
From source file:Main.java
public static Element fetchOrCreateElement(Document doc, Node parent, String tagName) { NodeList nl = parent.getChildNodes(); if (nl != null && nl.getLength() > 0) { for (int i = 0; i < nl.getLength(); ++i) { Node n = nl.item(i);//from w w w . j av a 2 s . c om if (n instanceof Element) { Element e = (Element) n; if (((Element) n).getTagName().equals(tagName)) { return e; } } } } // if get to here, not found element with this tagName, so create it return createElement(doc, parent, tagName); }
From source file:Main.java
public static Node removeChildren(Node parentNode) { if (parentNode != null) { NodeList list = parentNode.getChildNodes(); if (list != null) { for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); if (node != null) { parentNode.removeChild(node); }/*w w w . jav a2s . c o m*/ } } } return parentNode; }
From source file:Main.java
/** * Returns the first child node matching the {@code tagName}. *///from www . j a v a2s . co m public static Node findNode(Node parent, String tagName) { Node result = null; NodeList nl = parent.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { if (nl.item(i).getNodeName().equals(tagName)) { return nl.item(i); } } return result; }