List of usage examples for org.w3c.dom Node getFirstChild
public Node getFirstChild();
From source file:Utils.java
public static Element getFirstElement(Node parent) { Node n = parent.getFirstChild(); while (n != null && Node.ELEMENT_NODE != n.getNodeType()) { n = n.getNextSibling();// w w w . j a v a2s . com } if (n == null) { return null; } return (Element) n; }
From source file:Main.java
/** * @param n the node to look for text on * @return The conjoined values of all #text nodes immediately under this * node/* w w w . j ava 2s .c o m*/ */ public static String getText(Node n) { StringBuilder sb = new StringBuilder(); for (Node ele = n.getFirstChild(); ele != null; ele = ele.getNextSibling()) { String name = ele.getNodeName(); if (name.equalsIgnoreCase("#text")) { sb.append(ele.getNodeValue()); } } return sb.toString().trim(); }
From source file:Main.java
private static Element findElementinChilds(Node node, String uri, String nodeName) { for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() == Node.ELEMENT_NODE) { Element childElement = (Element) child; if (nodeName.equals(childElement.getLocalName()) && uri.equals(childElement.getNamespaceURI())) { return (Element) childElement; }/*from w w w . ja v a2 s . c o m*/ } Element located = findElementinChilds(child, uri, nodeName); if (located != null) { return located; } } return null; }
From source file:Main.java
/** * Finds the DOM node with specified name from a node list and returns * its content as a string./*from w w w .jav a 2 s. co m*/ * @param nodes Node list * @param name Node name * @return Content of the node as a string, or null if no node/content * @example * <pre name="test"> * Document d = createDomDocument(); * Node root = d.appendChild( d.createElement("Root") ); * Node n1 = root.appendChild( d.createElement("Node1") ); * n1.appendChild( d.createTextNode("Value1") ); * Element e2 = d.createElement("Node2"); * e2.appendChild( d.createTextNode("Value2") ); * Node n2 = root.appendChild( e2 ); * getNodeContent(root.getChildNodes(), "Node1") === "Value1"; * getNodeContent(root.getChildNodes(), "Node2") === "Value2"; * getNodeContent(root.getChildNodes(), "NotFound") === null; * </pre> */ public static String getNodeContent(NodeList nodes, String name) { Node node = getNodeByName(nodes, name); if (node == null || node.getFirstChild() == null) return null; return node.getTextContent(); }
From source file:Main.java
/** * Returns the child text from a DOM node. * @param node the node to parse//from www. jav a 2 s .c o m * @return the node text, or <tt>null</tt> if the node did not contain any text */ public static String getText(Node node) { StringBuilder s = null; Node child = node.getFirstChild(); while (child != null) { if (child.getNodeType() == Node.TEXT_NODE) { if (s == null) { s = new StringBuilder(); } s.append(((Text) child).getTextContent()); } else if (child.getNodeType() == Node.CDATA_SECTION_NODE) { if (s == null) { s = new StringBuilder(); } s.append(((CDATASection) child).getData()); } child = child.getNextSibling(); } return s == null ? null : s.toString(); }
From source file:Main.java
public static String getElementValue(Node elem) { if ((elem != null) && (elem.hasChildNodes())) { for (Node kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling()) { if (kid.getNodeType() == Node.TEXT_NODE) { return kid.getNodeValue(); }/*from ww w .j av a2 s . co m*/ } } return ""; }
From source file:Main.java
public static long readSubNodeLongValue(Node n, String name, long dflt) { if (name == null) return dflt; for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling()) if (name.equalsIgnoreCase(d.getNodeName())) return Long.parseLong(d.getNodeValue()); return dflt;//from w w w . j a va2 s. c om }
From source file:Main.java
/** * Returns the value of <code>Node</code>. This is done by assuming that * this node has one child that is a textnode. The value of this textnode is * returned./* w ww . j av a2 s. c om*/ * * @param node * The node that has to be examined. * @return The value of the node. */ public static String nodeValue(Node node) { if (node == null) return null; Node child = node.getFirstChild(); if (child == null) return null; return (child.toString().equals("") ? null : child.toString()); }
From source file:Main.java
public static String readSubNodeValue(Node n, String name, String dflt) { if (name == null) return dflt; for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling()) if (name.equalsIgnoreCase(d.getNodeName())) return d.getNodeValue(); return dflt;// ww w. jav a2s. c om }
From source file:Main.java
public static ArrayList<Element> getChildElements(Node node) { ArrayList<Element> l = new ArrayList<Element>(); for (Node childNode = node.getFirstChild(); childNode != null;) { if (childNode.getNodeType() == Node.ELEMENT_NODE) { Element elem = (Element) childNode; l.add(elem);/*from w ww . j a v a 2s.c o m*/ } Node nextChild = childNode.getNextSibling(); childNode = nextChild; } return l; }