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
/** * Helper method to get a list of only {@link Text} and {@link Element} typed {@link Node}s. * This is partially to workaround the difficulty of working with the {@link NodeList} object. * /*from w w w. jav a 2 s.c o m*/ * @param node * the node whose children to get * * @return the filtered list of child nodes */ private static List<Node> getChildNodes(Node node) { List<Node> children = new ArrayList<Node>(); NodeList nl = node.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node child = nl.item(i); short type = child.getNodeType(); if (type == Node.ELEMENT_NODE) { children.add(child); } else if (type == Node.TEXT_NODE) { String text = ((Text) child).getTextContent().trim(); if (text.length() > 0) { children.add(child); } } } return children; }
From source file:Main.java
public static void getEntityValues(Node node, Map map) { if (node instanceof EntityReference) { map.put(node.getNodeName(), node); }//w w w . j a v a 2 s. co m NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { getEntityValues(list.item(i), map); } }
From source file:Main.java
public static ArrayList<Node> findAllChildren(final Node n, final String name) { final ArrayList<Node> retVal = new ArrayList<Node>(); for (int i = 0; i < n.getChildNodes().getLength(); i++) { if (n.getChildNodes().item(i).getNodeName().equals(name)) { retVal.add(n.getChildNodes().item(i)); }/*from ww w. java 2s . c o m*/ } if (retVal.isEmpty()) { throw new IllegalStateException("no " + name + " children found in: " + n.getNodeName()); } return retVal; }
From source file:Main.java
public static boolean isSubTagExist(Node node, String tagName) { if (node == null) { return false; }/*from w w w . j a v a 2s .com*/ NodeList nodeList = node.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node n = nodeList.item(i); if (n instanceof Element) { if (((Element) n).getTagName().equals(tagName)) { return true; } } } return false; }
From source file:Main.java
/** * Get fisrt specified child node by tag name. * //ww w .jav a 2s. com * @param parent * the parent node * @param tagName * the tag name * @return the child node * @throws Exception * on error */ public static Node getFirstChildNode(Node parent, String tagName) throws Exception { if (null != parent) { NodeList childrenList = parent.getChildNodes(); int childrenCnt = childrenList.getLength(); for (int i = 0; i < childrenCnt; i++) { Node child = childrenList.item(i); if (child.getNodeName().equals(tagName)) { return child; } } } return null; }
From source file:Main.java
public static Collection<Node> getChildNodes(Node node) { Collection<Node> list = new LinkedList<Node>(); NodeList nl = node.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) list.add(nl.item(i));/*from w w w .j a v a 2s .c o m*/ return list; }
From source file:Main.java
public static List<Node> getChildNodes(Node node, String... elements) { List<Node> result = new ArrayList<Node>(); NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node childNode = childNodes.item(i); for (String element : elements) { if (element.equals(childNode.getNodeName())) { result.add(childNode);/*from w ww .j a va 2 s . c o m*/ } } } return result; }
From source file:Main.java
public static String getNormalizedText(Node node) { String res = ""; if (node.hasChildNodes()) { NodeList nl = node.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { if (nl.item(i).getNodeType() == Node.TEXT_NODE) { res += nl.item(i).getNodeValue(); } else { // ignore <SCRIPT> nodes ... if (!nl.item(i).getNodeName().equalsIgnoreCase("script")) res += getNormalizedText(nl.item(i)); }//from w ww . ja va2s . c o m } } return res; }
From source file:Main.java
/** * (A useful utility method from IBM developerworks) *///www. j a va2s .com public static String getTextContents(Node node) { NodeList childNodes; StringBuffer contents = new StringBuffer(); childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { if (childNodes.item(i).getNodeType() == Node.TEXT_NODE) { contents.append(childNodes.item(i).getNodeValue()); } } return contents.toString(); }
From source file:Main.java
/** * Returns a String containing the concatenation of text contained in the * given node. This includes Text node children, and Text node children of * Element nodes and their Element node children. * //from w ww .java2 s . c om * @param node a Node object * @return a concatenation of the descendent text */ public static String getChildrenText(Node node) { StringBuffer buf = new StringBuffer(); getChildrenText(node.getChildNodes(), buf); return buf.toString(); }