List of usage examples for org.w3c.dom Node getNodeType
public short getNodeType();
From source file:Main.java
/** * Returns the child text from a DOM node. * @param node the node to parse/*from w ww . j a v a2 s . c om*/ * @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
/** * A method to get the value of desire node from xml document * @param Node parent, xml's node object to get * @return String , value from provided node *//*from www .j a v a 2 s . c o m*/ static public String getNodeValue(Node parent) { String ret = ""; Node n = parent.getFirstChild(); while (n != null) { if (n.getNodeType() == Node.TEXT_NODE) { try { ret = n.getNodeValue().trim(); } catch (NullPointerException ex) { ret = ""; break; } } n = n.getNextSibling(); } return (ret); }
From source file:Main.java
/** Finds and returns the last child element node. */ public static Element getLastChildElement(Node parent) { if (parent == null) return null; // search for node Node child = parent.getLastChild(); while (child != null) { if (child.getNodeType() == Node.ELEMENT_NODE) { return (Element) child; }/*from ww w . j a va 2 s . c om*/ child = child.getPreviousSibling(); } // not found return null; }
From source file:Main.java
/** * This method returns the first non-null owner document of the Nodes in this Set. * This method is necessary because it <I>always</I> returns a * {@link Document}. {@link Node#getOwnerDocument} returns <CODE>null</CODE> * if the {@link Node} is a {@link Document}. * * @param xpathNodeSet/*from w w w . jav a2 s .c om*/ * @return the owner document */ public static Document getOwnerDocument(Set<Node> xpathNodeSet) { NullPointerException npe = null; for (Node node : xpathNodeSet) { int nodeType = node.getNodeType(); if (nodeType == Node.DOCUMENT_NODE) { return (Document) node; } try { if (nodeType == Node.ATTRIBUTE_NODE) { return ((Attr) node).getOwnerElement().getOwnerDocument(); } return node.getOwnerDocument(); } catch (NullPointerException e) { npe = e; } } throw new NullPointerException(npe.getMessage()); }
From source file:Main.java
public static int getCurrentPosition(final Node refNode) { if (refNode == null) { return -1; }/*from w w w . j ava 2 s . com*/ int counter = 0; Node current = refNode; while (current != null) { if (current.getNodeType() == Node.ELEMENT_NODE) { counter++; } current = current.getPreviousSibling(); } return counter; }
From source file:Main.java
/** * Copy one node to another node./* w w w.j a va 2 s . co m*/ * @param source source Node * @param dest destination Node * @return destination Node */ public static synchronized Node copyNode(Node source, Node dest) { if (source.getNodeType() == Node.TEXT_NODE) { Text tn = dest.getOwnerDocument().createTextNode(source.getNodeValue()); return tn; } Node attr = null; NamedNodeMap attrs = source.getAttributes(); if (attrs != null) { for (int i = 0; i < attrs.getLength(); i++) { attr = attrs.item(i); ((Element) dest).setAttribute(attr.getNodeName(), attr.getNodeValue()); } } Node child = null; NodeList list = source.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { child = list.item(i); if (!(child instanceof Text)) { Element en = dest.getOwnerDocument().createElementNS(child.getNamespaceURI(), child.getNodeName()); if (child.getNodeValue() != null) { en.setNodeValue(child.getNodeValue()); } Node n = copyNode(child, en); dest.appendChild(n); } else if (child instanceof CDATASection) { CDATASection cd = dest.getOwnerDocument().createCDATASection(child.getNodeValue()); dest.appendChild(cd); } else { Text tn = dest.getOwnerDocument().createTextNode(child.getNodeValue()); dest.appendChild(tn); } } return dest; }
From source file:Main.java
/** Finds and returns the last child node with the given name. */ public static Element getLastChildElement(Node parent, String elemName) { if (parent == null) return null; // search for node Node child = parent.getLastChild(); while (child != null) { if (child.getNodeType() == Node.ELEMENT_NODE) { if (child.getNodeName().equals(elemName)) { return (Element) child; }/*from www . j av a2s . c o m*/ } child = child.getPreviousSibling(); } // not found return null; }
From source file:Main.java
/** Finds and returns the last child node with the given qualified name. */ public static Element getLastChildElementNS(Node parent, String[][] elemNames) { // search for node Node child = parent.getLastChild(); while (child != null) { if (child.getNodeType() == Node.ELEMENT_NODE) { for (int i = 0; i < elemNames.length; i++) { String uri = child.getNamespaceURI(); if (uri != null && uri.equals(elemNames[i][0]) && child.getLocalName().equals(elemNames[i][1])) { return (Element) child; }/*ww w . ja v a 2s . co m*/ } } child = child.getPreviousSibling(); } // not found return null; }
From source file:Main.java
/** * @param child//from w w w .j a v a2s . co m * @return the single child element or null * @throws Exception if the child is present multiple times */ public static Element getChild(Element element, String child) throws Exception { NodeList nodes = element.getChildNodes(); Element ret = null; for (int i = 0; i < nodes.getLength(); i++) { Node childNode = nodes.item(i); if (childNode.getNodeName().equalsIgnoreCase(child) && childNode.getNodeType() == Node.ELEMENT_NODE) { if (ret != null) { throw new Exception("Child element '" + child + "' present multiple times"); } else { ret = (Element) childNode; } } } if (ret == null) { return null; } else { return ret; } }
From source file:Main.java
/** * Access all immediate child elements inside the given Element * * @param element the starting element, cannot be null. * @param elemName the name of the child element to look for, cannot be * null.//from www . ja v a 2 s. c o m * @return array of all immediate child element inside element, or * an array of size zero if no child elements are found. */ public static Element[] getChildElements(Element element) { NodeList list = element.getChildNodes(); int len = list.getLength(); ArrayList<Node> array = new ArrayList<Node>(len); for (int i = 0; i < len; i++) { Node n = list.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { array.add(n); } } Element[] elems = new Element[array.size()]; return (Element[]) array.toArray(elems); }