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 get_child_with_attr(Node parent, String child_name, String attr_name, String attr_value) { NodeList children = parent.getChildNodes(); for (int i = 0; i < children.getLength(); ++i) { Node child = children.item(i); if (child.getNodeName().equals(child_name)) { if (child instanceof Element) { Element e = (Element) child; if (e.getAttribute(attr_name).equals(attr_value)) return e; }//w ww .j av a 2s . c om } } return null; }
From source file:Main.java
/** * We get a object that connected text node of the child node and the CDATA section as character string. * @param node//w w w . j a v a2 s .c o m * @return */ public static String getChildText(Node node) { StringBuffer buf = new StringBuffer(); for (int i = 0; i < node.getChildNodes().getLength(); i++) { Node n = node.getChildNodes().item(i); if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.CDATA_SECTION_NODE) { buf.append(n.getNodeValue()); } } return buf.toString(); }
From source file:Main.java
/** * Remove any whitespace text nodes from the DOM. Calling this before saving * a formatted document will fix the formatting indentation of elements * loaded from a different document./*www. j a v a 2s.co m*/ * * @param node * Node to remove whitespace nodes from * @param deep * Should this method recurse into the node's children? */ public static void removeWhitespace(Node node, boolean deep) { NodeList children = node.getChildNodes(); int length = children.getLength(); for (int i = 0; i < length; i++) { Node child = children.item(i); if (child.getNodeType() == Node.TEXT_NODE && length > 1) { Node previous = child.getPreviousSibling(); Node next = child.getNextSibling(); if ((previous == null || previous.getNodeType() == Node.ELEMENT_NODE || previous.getNodeType() == Node.COMMENT_NODE) && (next == null || next.getNodeType() == Node.ELEMENT_NODE || next.getNodeType() == Node.COMMENT_NODE)) { String content = child.getTextContent(); if (content.matches("\\s*")) //$NON-NLS-1$ { node.removeChild(child); i--; length--; } } } else if (deep && child.getNodeType() == Node.ELEMENT_NODE) { removeWhitespace(child, deep); } } }
From source file:Main.java
public static final void setNodeValue(Node node, String value) { if (value == null) value = ""; NodeList childNodes = node.getChildNodes(); int len = childNodes == null ? 0 : childNodes.getLength(); for (int i = 0; i < len; i++) { Node n = childNodes.item(i); n.setNodeValue(value);// w w w. j av a2 s .c om } }
From source file:Main.java
public static Node getNodeChildByName(Node parentNode, String childNodeName) { Node childNode = null;//from w ww . j a v a 2 s .c o m NodeList childNodeList = parentNode.getChildNodes(); int len = childNodeList.getLength(); for (int i = 0; i < len; i++) { Node tmpChildNode = childNodeList.item(i); if (tmpChildNode.getNodeName().equals(childNodeName)) { return tmpChildNode; } } childNode = null; return childNode; }
From source file:Main.java
public static Map<String, Object> convertNodeToMap(Node node) { NodeList nodeList = node.getChildNodes(); Map<String, Object> map = new HashMap<String, Object>(nodeList.getLength()); for (int i = 0; i < nodeList.getLength(); i++) { Node nodec = nodeList.item(i); String key = nodec.getNodeName(); Object value = null;//from ww w. j a v a 2 s . com if (nodec.hasChildNodes()) { NodeList nodeListc = nodec.getChildNodes(); if (nodeListc.getLength() == 1) { Node noded = nodeListc.item(0); short type = noded.getNodeType(); if (type == 3 || type == 4) { value = noded.getNodeValue(); } if (noded.getNodeType() == 1) { value = convertNodeToMap(nodec); } } else { value = convertNodeToMap(nodec); } } map.put(key, value); } return map; }
From source file:Main.java
private static void findAllProcessingIstructions(Node node, String name, List<ProcessingInstruction> result) { NodeList nodeList = node.getChildNodes(); if (nodeList == null) { return;//from ww w . j a v a 2 s .c om } for (int i = 0; i < nodeList.getLength(); ++i) { Node n = nodeList.item(i); if (n.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) { if (name == null || name.length() == 0 || n.getNodeName().equals(name)) { result.add((ProcessingInstruction) n); } } findAllProcessingIstructions(n, name, result); } }
From source file:Main.java
/** * @param node/* w ww . ja va 2s . c o m*/ * @param name * @return all children nodes whose names contain <code>like</code> */ public static List<Node> findChildrenLike(Node node, String like) { List<Node> ret = new LinkedList<Node>(); NodeList nl = node.getChildNodes(); int len = nl.getLength(); Node child; for (int i = 0; i < len; i++) { child = nl.item(i); if (child.getNodeName().indexOf(like) > 0) { ret.add(child); } } return ret; }
From source file:Main.java
/** * @param node/*from w w w.j a v a 2 s . c o m*/ * @param name * @return all children nodes with the given name */ public static List<Node> findChildren(Node node, String name) { List<Node> ret = new LinkedList<Node>(); NodeList nl = node.getChildNodes(); int len = nl.getLength(); Node child; for (int i = 0; i < len; i++) { child = nl.item(i); if (name.equals(child.getNodeName())) { ret.add(child); } } return ret; }
From source file:Main.java
public static List<Element> getRealChilds(Node node) { List<Element> interlayer = new ArrayList<Element>(); NodeList l = node.getChildNodes(); for (int i = 0; i < l.getLength(); i++) { Node n = l.item(i);/*from ww w . j a v a 2s. c om*/ if (n instanceof Element) { interlayer.add(((Element) n)); } } return interlayer; }