List of usage examples for org.w3c.dom Node getNextSibling
public Node getNextSibling();
From source file:Main.java
/** * Method getFullTextChildrenFromElement * * @param element//w w w . j av a 2 s . co m * @return the string of children */ public static String getFullTextChildrenFromElement(Element element) { StringBuilder sb = new StringBuilder(); Node child = element.getFirstChild(); while (child != null) { if (child.getNodeType() == Node.TEXT_NODE) { sb.append(((Text) child).getData()); } child = child.getNextSibling(); } return sb.toString(); }
From source file:Main.java
/** * Get the elements with the given name. * @param element//from www.j a v a2 s .c om * @param name * @return array, never <code>null</code> */ public static Element[] getChildElementsNamed(Element element, String name) { if (element == null) return new Element[0]; NodeList nodeList = element.getChildNodes(); List<Element> elements = new ArrayList<Element>(); Node node = nodeList.item(0); while (node != null) { if (node instanceof Element && name.equals(node.getNodeName())) { elements.add((Element) node); } node = node.getNextSibling(); } return (Element[]) elements.toArray(new Element[elements.size()]); }
From source file:Main.java
/** * Returns next sibling element node//from w ww .ja v a 2s. c o m * @param element * @return */ protected static Element getNextSiblingElement(Element element) { try { Node tmpNode = null; tmpNode = element.getNextSibling(); while (tmpNode.getNodeType() != Node.ELEMENT_NODE) { //fix for structure that has more than two parents null if (tmpNode.getNextSibling() == null) tmpNode = tmpNode.getParentNode(); tmpNode = tmpNode.getNextSibling(); } return (Element) tmpNode; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
/** Look for Element node if given name. * <p>/* w w w. j a va 2 s . c o m*/ * 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
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 ww . j av a 2s. c o m node = node.getNextSibling(); } return null; }
From source file:Main.java
/** * Get the first child element with a specified name. * If the starting node is a Document, use the document element * as the starting point. Only first-generation children of the * starting node are searched.//from w w w . j a va 2s . c o m * @param node the starting node. * @param name the name of the child to find. * @return the first child element with the specified name, or null * if the starting node is null or if no child with the name exists. */ public static Element getFirstNamedChild(Node node, String name) { if (node == null) return null; if (node instanceof Document) node = ((Document) node).getDocumentElement(); if (!(node instanceof Element)) return null; Node child = node.getFirstChild(); while (child != null) { if ((child instanceof Element) && child.getNodeName().equals(name)) { return (Element) child; } child = child.getNextSibling(); } return null; }
From source file:Main.java
/** * Get all the direct children elements of an element. * * @param parent//from w ww . j a v a 2 s . c o m * The parent element. * * @return A list of Element's. */ public static List<Element> getElements(final Element parent) { final LinkedList<Element> list = new LinkedList<Element>(); Node node = parent.getFirstChild(); while (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE) { list.add((Element) node); } node = node.getNextSibling(); } return list; }
From source file:Main.java
/** * @param n1 first Node to test/* www . j a v a 2 s. co m*/ * @param n2 second Node to test * @return true if a deep compare show the same children and attributes in the same order */ public static boolean equals(Node n1, Node n2) { // compare type if (!n1.getNodeName().equals(n2.getNodeName())) return false; // compare attributes NamedNodeMap nnm1 = n1.getAttributes(); NamedNodeMap nnm2 = n2.getAttributes(); if (nnm1.getLength() != nnm2.getLength()) return false; for (int i = 0; i < nnm1.getLength(); i++) { Node attr1 = nnm1.item(i); if (!getAttribute(n1, attr1.getNodeName()).equals(getAttribute(n2, attr1.getNodeName()))) return false; } // compare children Node c1 = n1.getFirstChild(); Node c2 = n2.getFirstChild(); for (;;) { while ((c1 != null) && c1.getNodeName().startsWith("#")) c1 = c1.getNextSibling(); while ((c2 != null) && c2.getNodeName().startsWith("#")) c2 = c2.getNextSibling(); if ((c1 == null) && (c2 == null)) break; if ((c1 == null) || (c2 == null)) return false; if (!equals(c1, c2)) return false; c1 = c1.getNextSibling(); c2 = c2.getNextSibling(); } return true; }
From source file:Utils.java
public static Element getNextElement(Element el) { Node nd = el.getNextSibling(); while (nd != null) { if (nd.getNodeType() == Node.ELEMENT_NODE) { return (Element) nd; }/* ww w . jav a 2 s. co m*/ nd = nd.getNextSibling(); } return null; }
From source file:Main.java
/** * Sets the text value of an Element. if current text of the element is * replaced with the new text if text is null any * current text value is deleted.//from ww w . j a v a 2s .c o m * * @param elem the Element for which the text value should be set * @param text the new text value of the element * @return true if the text could be set or false otherwise */ static public boolean setElementText(Node elem, Object text) { if (elem == null) return false; // Fehler // Find Text Node node = elem.getFirstChild(); while (node != null) { // Find all Text nodes if (node.getNodeType() == Node.TEXT_NODE) break; // gefunden node = node.getNextSibling(); } if (node != null) { // Set or remove text if (text != null) node.setNodeValue(text.toString()); else elem.removeChild(node); } else if (text != null) { // Add Text elem.appendChild(elem.getOwnerDocument().createTextNode(text.toString())); } return true; }