List of usage examples for org.w3c.dom Node getNextSibling
public Node getNextSibling();
From source file:Main.java
/** * Returns the concatenated child text of the specified node. * This method only looks at the immediate children of type * Node.TEXT_NODE or the children of any child * node that is of type Node.CDATA_SECTION_NODE * for the concatenation./* w ww. j a v a2 s . c om*/ * * @param node The node to look at. */ public static String getChildText(Node node) { // is there anything to do? if (node == null) { return null; } // concatenate children text StringBuffer str = new StringBuffer(); Node child = node.getFirstChild(); while (child != null) { short type = child.getNodeType(); if (type == Node.TEXT_NODE) { str.append(child.getNodeValue()); } else if (type == Node.CDATA_SECTION_NODE) { str.append(getChildText(child)); } child = child.getNextSibling(); } // return text value return str.toString(); }
From source file:Main.java
/** * Builds an XPath string referencing the given node relative to it's parent. * /* w w w . j a va 2 s. c o m*/ * @param node * @param context namespace context used to determine correct namespace prefixes, see * {@link SignavioNamespaceContext} * @return a relative XPath string or null (if no node given) */ private static String getNodeString(Node node, NamespaceContext context) { if (node == null) { return null; } // get qualified name String nodeName = node.getLocalName(); if (nodeName == null) nodeName = node.getNodeName(); if (node.getNamespaceURI() != null) { String prefix = context.getPrefix(node.getNamespaceURI()); nodeName = prefix + ":" + node.getLocalName(); } if (node instanceof Attr) { return "@" + nodeName; } else if (node instanceof Text) { nodeName = "text()"; } // determine position Node current = node; while (current.getPreviousSibling() != null) { current = current.getPreviousSibling(); } int position = 1; while (current != node) { if (current.getNodeName().equals(node.getNodeName())) position++; current = current.getNextSibling(); } return nodeName + "[" + position + "]"; }
From source file:Main.java
/** Finds and returns the first child node with the given name. */ public static Element getFirstChildElement(Node parent, String elemNames[]) { // search for node Node child = parent.getFirstChild(); while (child != null) { if (child.getNodeType() == Node.ELEMENT_NODE) { for (int i = 0; i < elemNames.length; i++) { if (child.getNodeName().equals(elemNames[i])) { return (Element) child; }/* w ww . jav a 2 s.co m*/ } } child = child.getNextSibling(); } // not found return null; }
From source file:Main.java
/** * Return the text (node value) of the first node under this, works best if normalized. */// ww w .j a v a 2s .com public static String elementValue(Element element) { if (element == null) return null; // make sure we get all the text there... element.normalize(); Node textNode = element.getFirstChild(); if (textNode == null) return null; StringBuffer valueBuffer = new StringBuffer(); do { if (textNode.getNodeType() == Node.CDATA_SECTION_NODE || textNode.getNodeType() == Node.TEXT_NODE) { valueBuffer.append(textNode.getNodeValue()); } } while ((textNode = textNode.getNextSibling()) != null); return valueBuffer.toString(); }
From source file:Main.java
/** * Finds and returns the first child node with the given name and * attribute name, value pair./*from w w w . ja v a 2s.c om*/ */ public static Element getFirstChildElement(Node parent, String elemName, String attrName, String attrValue) { // search for node Node child = parent.getFirstChild(); while (child != null) { if (child.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) child; if (element.getNodeName().equals(elemName) && element.getAttribute(attrName).equals(attrValue)) { return element; } } child = child.getNextSibling(); } // not found return null; }
From source file:XMLUtils.java
public static void removeContents(Node parent) { Node node = parent.getFirstChild(); while (node != null) { parent.removeChild(node);//from ww w . j a v a 2s .c o m node = node.getNextSibling(); } }
From source file:Main.java
/** * Finds the first (direct) child Element with a given tag name. * /*from w w w.j a v a2s . c om*/ * @param parent the parent element below which to search the child * @param tagName the (tag) name of the desired child element * @return the child element if an element of that name existed, or null otherwise */ static public Element findFirstChild(Node parent, String tagName) { // Child Element suchen if (parent == null) return null; Node node = parent.getFirstChild(); while (node != null) { // Find all Element nodes if (node.getNodeType() == Node.ELEMENT_NODE) { // check name Element elem = (Element) node; if (tagName.equalsIgnoreCase(elem.getTagName())) return elem; // found } node = node.getNextSibling(); } return null; // not found! }
From source file:Main.java
/** * Return a List of Element objects that have the given name and are immediate children of the * given element; if name is null, all child elements will be included. *//*from w w w . j a v a 2 s . c o m*/ public static List<Element> childElementList(Element element, String childElementName) { if (element == null) return null; List<Element> elements = new LinkedList<Element>(); Node node = element.getFirstChild(); if (node != null) { do { if (node.getNodeType() == Node.ELEMENT_NODE && (childElementName == null || childElementName.equals(node.getNodeName()))) { Element childElement = (Element) node; elements.add(childElement); } } while ((node = node.getNextSibling()) != null); } return elements; }
From source file:Main.java
/** * @param sibling//w ww . ja v a2s .c om * @param uri * @param nodeName * @param number * @return nodes with the constrain */ public static Element selectNode(Node sibling, String uri, String nodeName, int number) { while (sibling != null) { if (sibling.getNamespaceURI() != null && sibling.getNamespaceURI().equals(uri) && sibling.getLocalName().equals(nodeName)) { if (number == 0) { return (Element) sibling; } number--; } sibling = sibling.getNextSibling(); } return null; }
From source file:Main.java
/** * Return the first child Element with the given name; if name is null returns the first element. *///from w ww. j a va 2 s.co m public static Element firstChildElement(Element element, String childElementName) { if (element == null) return null; // get the first element with the given name Node node = element.getFirstChild(); if (node != null) { do { if (node.getNodeType() == Node.ELEMENT_NODE && (childElementName == null || childElementName.equals(node.getNodeName()))) { Element childElement = (Element) node; return childElement; } } while ((node = node.getNextSibling()) != null); } return null; }