List of usage examples for org.w3c.dom Node getNodeType
public short getNodeType();
From source file:Main.java
public static Element getFirstChild(Element paramElement, String paramString) { NodeList localNodeList = paramElement.getChildNodes(); for (int i = 0; i < localNodeList.getLength(); i++) { Node localNode = localNodeList.item(i); if ((localNode != null) && (localNode.getNodeType() == 1) && (((Element) localNode).getTagName().equals(paramString))) return (Element) localNode; }//from www .ja v a2s. c om return null; }
From source file:Main.java
/** Finds and returns the last child element node. * Overload previous method for non-Xerces node impl. *//*from w w w . j ava2 s . co m*/ public static Element getLastChildElement(Node parent) { // search for node Node child = parent.getLastChild(); while (child != null) { if (child.getNodeType() == Node.ELEMENT_NODE) { return (Element) child; } child = child.getPreviousSibling(); } // not found return null; }
From source file:Main.java
public static Element getElement2(Element parentElement, String nodeName) { Node node = parentElement.getFirstChild(); while (null != node) { if ((Element.ELEMENT_NODE == node.getNodeType()) && (nodeName.equals(node.getNodeName()))) { return (Element) node; }/* w w w.j av a2 s. c om*/ node = node.getNextSibling(); } return null; }
From source file:Main.java
/** * Searches the node for content of type Long. If non-long content is found, * it logs a warning and returns null./* w w w .jav a 2 s . co m*/ */ public static String extractStringFromElement(Node element) { if (element == null) { return null; } else if (element.getFirstChild() == null) { return null; } else if (element.getFirstChild().getNodeValue() == null) { return null; } else { // Get all the children NodeList children = element.getChildNodes(); StringBuffer output = new StringBuffer(); for (int n = 0; n < children.getLength(); n++) { Node child = children.item(n); if (child.getNodeType() == Node.ENTITY_REFERENCE_NODE) { output.append(child.getFirstChild().getNodeValue()); } else if (child.getNodeType() == Node.CDATA_SECTION_NODE) { output.append(child.getFirstChild().getNodeValue()); } else if (child.getNodeType() == Node.ENTITY_NODE) { output.append(child.getFirstChild().getNodeValue()); } else if (child.getNodeType() == Node.TEXT_NODE) { output.append(child.getNodeValue()); } } return output.toString().trim(); } }
From source file:Main.java
/** * Searches the node for content of type Long. If non-long content is found, * it logs a warning and returns null.//from ww w . ja v a 2 s. c o m */ public static String[] extractStringArrayFromElement(Node element) { // Get all the children NodeList children = element.getChildNodes(); List<String> output = new ArrayList<String>(); for (int n = 0; n < children.getLength(); n++) { Node child = children.item(n); if (child.getNodeType() == Node.ELEMENT_NODE) { output.add(extractStringFromElement(child)); } } return output.toArray(new String[output.size()]); }
From source file:Main.java
public static final String getComment(Element elem) { StringBuffer sb = new StringBuffer(); Node node = elem.getPreviousSibling(); while (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE) { break; }// ww w . j a v a 2 s . c o m if (node.getNodeType() == Node.COMMENT_NODE) { if (sb.length() > 0) { sb.insert(0, '\n'); sb.insert(0, ((Comment) node).getData()); } else { sb.append(((Comment) node).getData()); } } node = node.getPreviousSibling(); } return sb.toString(); }
From source file:Main.java
public static final String getComment(Element elem) { StringBuilder sb = new StringBuilder(); Node node = elem.getPreviousSibling(); while (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE) { break; }//from w w w . jav a 2 s.c o m if (node.getNodeType() == Node.COMMENT_NODE) { if (sb.length() > 0) { sb.insert(0, '\n'); sb.insert(0, ((Comment) node).getData()); } else { sb.append(((Comment) node).getData()); } } node = node.getPreviousSibling(); } return sb.toString(); }
From source file:Main.java
/** Look for Element node if given name. * <p>// w ww .ja va 2 s .c om * Checks the node and its siblings. * Does not descent down the 'child' links. * @param node Node where to start. * @param name Name of the nodes to look for. * @return Returns node or the next matching sibling or null. */ final public static Element findFirstElementNode(Node node, final String name) { while (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name)) return (Element) node; node = node.getNextSibling(); } return null; }
From source file:Main.java
/** * @param name The name of the child elements you want * @return a List of child Elements// w ww.ja v a2 s .c o m */ public static List<Element> getChildren(Element element, String name) throws Exception { NodeList nodes = element.getChildNodes(); ArrayList<Element> ret = new ArrayList<Element>(nodes.getLength()); for (int i = 0; i < nodes.getLength(); i++) { Node childNode = nodes.item(i); if (childNode.getNodeName().equals(name) && childNode.getNodeType() == Node.ELEMENT_NODE) { ret.add((Element) childNode); } } return ret; }
From source file:Main.java
/** * Prints out the DOM tree./*from www . j ava 2 s . c o m*/ * * @param n * the parent node to start printing from * @param prefix * string to append to output, should not be null */ public static void printDom(Node n, String prefix, StringBuilder sb) { String outString = prefix; if (n.getNodeType() == Node.TEXT_NODE) { outString += "'" + n.getTextContent().trim() + "'"; } else { outString += n.getNodeName(); } sb.append(outString); sb.append("\n"); NodeList children = n.getChildNodes(); int length = children.getLength(); for (int i = 0; i < length; i++) { printDom(children.item(i), prefix + " ", sb); } }