List of usage examples for org.w3c.dom Node getNextSibling
public Node getNextSibling();
From source file:Main.java
/** * Get the next sibling element of the specified element, null if it has no other siblings. * /*from w ww. j a va2s . c o m*/ * @param element The element to search siblings for. * @return The next sibling element, null if it has none. */ public static Element getNextSibling(Element element) { if (element == null) return null; Node node = element.getNextSibling(); while (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE) return (Element) node; node = node.getNextSibling(); } return null; }
From source file:Main.java
/** * Get the Element children of an element as an array. * @param element//from www. j a v a2 s.co m * @return non-<code>null</code> array */ public static Element[] getChildElements(Element element) { NodeList childNodes = element.getChildNodes(); List<Element> kids = new ArrayList<Element>(); Node node = childNodes.item(0); while (node != null) { if (node instanceof Element) { kids.add((Element) node); } node = node.getNextSibling(); } return (Element[]) kids.toArray(new Element[kids.size()]); }
From source file:Main.java
public static Set<Node> populateNodes(Node node, Set<Node> nodes) { if (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE) { nodes.add(node);/*w ww .j a va 2 s .c om*/ } populateNodes(node.getNextSibling(), nodes); } return nodes; }
From source file:Main.java
/** * Rename an element, replacing it in its document. * @param element the element to rename. * @param name the new element name./* w ww . j a va 2 s .c o m*/ * @return the renamed element. */ public static Element renameElement(Element element, String name) { if (element.getNodeName().equals(name)) return element; Element el = element.getOwnerDocument().createElement(name); //Copy the attributes NamedNodeMap attributes = element.getAttributes(); int nAttrs = attributes.getLength(); for (int i = 0; i < nAttrs; i++) { Node attr = attributes.item(i); el.setAttribute(attr.getNodeName(), attr.getNodeValue()); } //Copy the children Node node = element.getFirstChild(); while (node != null) { Node clone = node.cloneNode(true); el.appendChild(clone); node = node.getNextSibling(); } //Replace the element element.getParentNode().replaceChild(el, element); return el; }
From source file:Main.java
/** * Removes text nodes that only contains whitespace. The conditions for * removing text nodes, besides only containing whitespace, are: If the * parent node has at least one child of any of the following types, all * whitespace-only text-node children will be removed: - ELEMENT child - * CDATA child - COMMENT child/* w w w. ja va 2 s. c o m*/ * * The purpose of this is to make the format() method (that use a * Transformer for formatting) more consistent regarding indenting and line * breaks. */ public static void cleanEmptyTextNodes(Node parentNode) { boolean removeEmptyTextNodes = false; Node childNode = parentNode.getFirstChild(); while (childNode != null) { removeEmptyTextNodes |= checkNodeTypes(childNode); childNode = childNode.getNextSibling(); } if (removeEmptyTextNodes) { removeEmptyTextNodes(parentNode); } }
From source file:Main.java
/** * Getter for a specific text content of the given node. * @param node Node./*ww w . jav a 2 s.c o m*/ * @param name Name of element tags around the text content. * @return Returns the content or null if no content with the given * name exists. */ static public String getTextContent(Node node, String name) { node = node.getFirstChild(); while (node != null) { if (node.getNodeName().equals(name)) return (node.getTextContent()); node = node.getNextSibling(); } return (null); }
From source file:Main.java
/** * Get the text from adjacent CharacterData nodes. This will take the text from the passed in node, * and merge it with the text from the next siblings until it reaches a node which is not CharacterData. * //w w w . j a v a2 s . c o m * Note that this is necessary during loading, as the result of saving a CDATA node may in fact * be more than one CDATA node in the output. This will happen because any time the substring "]]>" * (the substring to end a CDATA node) appears while outputting the CDATA text, the node transformer * will end the current node at "]]" and start a new one CDATA node to avoid screwing up the output. * * Note: CDATA extends CharacterData (so CDATA != CharacterData) * * @param firstNode the first of [0..n] CharacterData nodes whose text to merge and return. * @param resultBuffer the buffer into which to add the result of merging the CharacterData text. * Warning: anything in this buffer will be overwritten! * @return the first sibling node which is not a CharacterData node, or null is there isn't any. */ public static Node getAdjacentCharacterData(Node firstNode, StringBuilder resultBuffer) { // Clear the result buffer resultBuffer.setLength(0); // merge adjacent CDATA nodes Node codeNode = firstNode; while (codeNode != null && codeNode instanceof CharacterData) { resultBuffer.append(((CharacterData) codeNode).getData()); codeNode = codeNode.getNextSibling(); } return codeNode; }
From source file:Main.java
public static String getTextData(Node node) { if (!node.hasChildNodes()) { return null; }/*w w w . j av a2 s . c o m*/ Node child = node.getFirstChild(); while (child != null && child.getNodeType() != Node.TEXT_NODE && child.getNodeType() != Node.CDATA_SECTION_NODE) { child = child.getNextSibling(); } if (child == null) { return null; } if (child.getNodeType() == Node.TEXT_NODE) { return ((Text) child).getData(); } else { return ((CDATASection) child).getData(); } }
From source file:Main.java
/** * Returns first DOM node of type element contained within given element. * /*from w w w .ja va 2 s . c o m*/ * @param elem element */ public static Element getContainedElement(Element elem) { Node n = elem.getFirstChild(); while (n != null) { if (n.getNodeType() == Node.ELEMENT_NODE) { Element e = (Element) n; return e; } n = n.getNextSibling(); } return null; }
From source file:Main.java
/** * Find the next sibling element./*from w ww .j ava 2s .c om*/ * * @param startNode XML start node. * @return the next sibling element. */ protected static Element findNextSibling(Node startNode) { Node node = null; Element returnElement = null; // Find the next sibling element for (node = startNode; node != null && returnElement == null; node = node.getNextSibling()) { // If this node is an element node, then return it if (node.getNodeType() == Node.ELEMENT_NODE) { returnElement = (Element) node; } } // Return next sibling element return (Element) returnElement; }