List of usage examples for org.w3c.dom Node getNodeType
public short getNodeType();
From source file:Main.java
/**gets string associated with a node * @param node node to get string for/*from w w w . ja v a2 s . c om*/ * @param string associated with the input node*/ static public String getAssociatedString(Node node) { // Has to be a real Node if (node == null) return null; // Must have one and only one string associted with it NodeList children = node.getChildNodes(); if (children.getLength() > 1) { System.out.println("XMLUtils: Node has more than one child"); return null; } Node firstChild = children.item(0); if (firstChild.getNodeType() != Node.TEXT_NODE) { System.out.println("XMLUtils: Node is not a text node"); System.out.println( "XMLUtils: Node = " + firstChild.getNodeName() + ", Parent Node = " + node.getNodeName()); return null; } String stringToReturn = firstChild.getNodeValue().trim(); if (stringToReturn.equals("")) { System.out.println("XMLUtils: Trimmed string is empty"); return null; } else return stringToReturn; }
From source file:Main.java
/** * Returns the first direct child element for the given parent node, which * matches the given tagName (probably faster than retrieving all children first). * @param parent the parent node under which to search for the element * @param tagName the tag name of the element to find * @return Element - the first found Element or null if no such Element is present *//*from w w w. j a v a2 s. com*/ public static Element findFirstChildElement(Node parent, String tagName) { Node n = parent.getFirstChild(); do { if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().equals(tagName))) { return (Element) n; } // Android Workaround try { n = n.getNextSibling(); } catch (IndexOutOfBoundsException e) { n = null; } } while (n != null); return null; }
From source file:Main.java
/** * Hack...since DOM reads newlines as textnodes we want to strip out those nodes to make it easier to use the tree. *//* w w w. j a v a 2s.co m*/ public static void stripEmptyTextNodes(Node n) { NodeList children = n.getChildNodes(); int length = children.getLength(); for (int i = 0; i < length; i++) { Node c = children.item(i); if (!c.hasChildNodes() && c.getNodeType() == Node.TEXT_NODE && c.getTextContent().trim().length() == 0) { n.removeChild(c); i--; length--; children = n.getChildNodes(); } else { stripEmptyTextNodes(c); } } }
From source file:Main.java
/*************************************************************************** * Sets the value of the first child under the given element with the given * name. If the child has no nodes, one is created. * // w w w .j a va 2 s . co m * @param doc * @param e * @param name * @param value * @return * @throws Exception **************************************************************************/ public static boolean setChildValueByName(Document doc, Element e, String name, String value) throws Exception { NodeList childNodes = e.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); if (node.getNodeType() != Node.ELEMENT_NODE) continue; if (node.getNodeName().equals(name)) { Node child = node.getFirstChild(); if (child == null) ((Element) node).appendChild(doc.createTextNode(value)); else child.setNodeValue(value); return true; } } return false; }
From source file:XMLUtils.java
/** * Extract all text children of an element *///from w ww.j a v a 2 s. c o m public static String extractTextChildren(Element parentNode) { NodeList childNodes = parentNode.getChildNodes(); String result = new String(); for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); if (node.getNodeType() == Node.TEXT_NODE) { result += node.getNodeValue(); } } return result; }
From source file:Main.java
public static int getCountOfChildNodes(Node node, String nodeName) { int count = 0; if (node.getNodeType() == Node.ELEMENT_NODE) { NodeList nodeList = node.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node n = nodeList.item(i); if (n.getNodeName().equals(nodeName)) { count++;//w w w . ja v a2s . co m } } } return count; }
From source file:Main.java
/** * Extracts from the given Element the first child having the given name. * * @param element//from w w w .ja va 2s. c o m * @param name * @return the first child with the given name or null if none */ public static Element firstChild(Element element, String name) { NodeList nodeList = element.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node current = nodeList.item(i); if (current.getNodeType() == Node.ELEMENT_NODE && current.getNodeName().equals(name)) { return (org.w3c.dom.Element) current; } } return null; }
From source file:Main.java
/** * Method getStrFromNode// www .j a v a2 s .co m * * @param xpathnode * @return the string for the node. */ public static String getStrFromNode(Node xpathnode) { if (xpathnode.getNodeType() == Node.TEXT_NODE) { // we iterate over all siblings of the context node because eventually, // the text is "polluted" with pi's or comments StringBuilder sb = new StringBuilder(); for (Node currentSibling = xpathnode.getParentNode() .getFirstChild(); currentSibling != null; currentSibling = currentSibling.getNextSibling()) { if (currentSibling.getNodeType() == Node.TEXT_NODE) { sb.append(((Text) currentSibling).getData()); } } return sb.toString(); } else if (xpathnode.getNodeType() == Node.ATTRIBUTE_NODE) { return ((Attr) xpathnode).getNodeValue(); } else if (xpathnode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) { return ((ProcessingInstruction) xpathnode).getNodeValue(); } return null; }
From source file:Main.java
public static Element getChild(Element node, String tagName) { Element element = null;//w w w . j a v a2 s.co m do { if (node == null) { break; } NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node child = childNodes.item(i); if (child.getNodeType() == Node.ELEMENT_NODE) { if (child.getNodeName().equals(tagName)) { element = (Element) child; break; } } } } while (false); return element; }
From source file:Main.java
/** * List all child nodes with name sName//from www . j av a 2 s. c o m * NOTE: we assume no same name nodes are nested. * @param node * @param sName * @return Element */ public static ArrayList<Element> listChildElementsByName(Node node, String sName) { ArrayList<Element> aNodes = new ArrayList<Element>(); NodeList nl = node.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node n = nl.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { if (sName.equals(n.getNodeName())) { aNodes.add((Element) n); } else { ArrayList<Element> nextNodes = listChildElementsByName(n, sName); if (nextNodes != null) aNodes.addAll(nextNodes); } } // Don't search anything but elements } return aNodes; }