List of usage examples for org.w3c.dom Node getNextSibling
public Node getNextSibling();
From source file:Main.java
/** * Returns the occurrences of a given node/element. Checks whether the previous/following * elements have the same name./* w w w. ja v a 2s. c o m*/ * * @param node The node whose occurrences should be determined. * @return The occurrences of node. Greater than or equal to 1. */ public static int getOccurs(Node node) { int occurs = 1; String nodeName = node.getNodeName(); Node prevSib = node.getPreviousSibling(); while (prevSib != null) { if (prevSib.getNodeName().compareTo(nodeName) == 0 && prevSib.getNodeType() == Node.ELEMENT_NODE) { occurs++; prevSib = prevSib.getPreviousSibling(); } else { prevSib = null; } } Node nextSib = node.getNextSibling(); while (nextSib != null) { if (nextSib.getNodeName().compareTo(nodeName) == 0 && nextSib.getNodeType() == Node.ELEMENT_NODE) { occurs++; nextSib = nextSib.getNextSibling(); } else { nextSib = null; } } return occurs; }
From source file:Main.java
/** Finds and returns the first child node with the given qualified name. */ public static Element getFirstChildElementNS(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++) { String uri = child.getNamespaceURI(); if (uri != null && uri.equals(elemNames[i][0]) && child.getLocalName().equals(elemNames[i][1])) { return (Element) child; }/* w w w. j a v a 2s. c om*/ } } child = child.getNextSibling(); } // not found return null; }
From source file:importer.handler.post.stages.Discriminator.java
static Element firstSibling(Element elem) { Node start = elem; while (start.getPreviousSibling() != null) start = start.getPreviousSibling(); while (start.getNodeType() != Node.ELEMENT_NODE) start = start.getNextSibling(); return (start.getNodeType() == Node.ELEMENT_NODE) ? (Element) start : null; }
From source file:com.mediaworx.xmlutils.XmlHelper.java
/** * Removes all empty or whitespace only text nodes from the given parent node. * @param parentNode the parent node to be cleared of empty or whitespace only text nodes *//* w w w . j a v a 2 s . c o m*/ private static void removeEmptyTextNodes(Node parentNode) { Node childNode = parentNode.getFirstChild(); while (childNode != null) { // grab the "nextSibling" before the child node is removed Node nextChild = childNode.getNextSibling(); short nodeType = childNode.getNodeType(); if (nodeType == Node.TEXT_NODE) { boolean containsOnlyWhitespace = childNode.getNodeValue().trim().isEmpty(); if (containsOnlyWhitespace) { parentNode.removeChild(childNode); } } childNode = nextChild; } }
From source file:Main.java
/** * Returns the child text from a DOM node. * @param node the node to parse// w ww . ja va 2 s . co m * @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
public static String getXpathExpressionValue(Object xprContext, String xpExpression) throws XPathExpressionException { XPath xpath = XPathFactory.newInstance().newXPath(); XPathExpression xpe = xpath.compile(xpExpression); Node valueNode = (Node) xpe.evaluate(xprContext, XPathConstants.NODE); String value = null;//w w w.ja v a2 s . co m if (valueNode != null) value = valueNode.getNodeValue(); if (value != null) { // if the node is a text node - then we trim and (potentially) look for CDATA if (valueNode.getNodeType() == Node.TEXT_NODE) { value = value.trim(); // look for CDATA if we got nothing (stupid whitespace) if (value.length() == 0) { Node siblingForCDATA = valueNode.getNextSibling(); if (siblingForCDATA.getNodeType() == Node.CDATA_SECTION_NODE) { value = siblingForCDATA.getNodeValue(); } } } } return value; }
From source file:Main.java
/** * Returns all child character data of the given element, including CDATA sections but NOT entity references. * @param parentEl the parent element./* ww w . j a va 2s . c om*/ * @return the child character data of the element, or null if the element is null. */ static public String getChildCharacterData(Element parentEl) { if (parentEl == null) { return null; } Node tempNode = parentEl.getFirstChild(); StringBuffer strBuf = new StringBuffer(); CharacterData charData; while (tempNode != null) { switch (tempNode.getNodeType()) { case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: charData = (CharacterData) tempNode; strBuf.append(charData.getData()); break; // case Node.ENTITY_REFERENCE_NODE : strBuf.append("&").append(tempNode.getNodeName()).append(";"); } tempNode = tempNode.getNextSibling(); } return strBuf.toString(); }
From source file:Main.java
private static String getMetaScriptType(Document doc) { // Can not just do a Document.getElementsByTagName(String) as this // needs/*from ww w . ja va 2 s. c o m*/ // to be relatively fast. List metas = new ArrayList(); // check for META tags under the Document Node html = null; Node head = null; Node child = null; // ---------------------------------------------------------------------- // (pa) 20021217 // cmvc defect 235554 // performance enhancement: using child.getNextSibling() rather than // nodeList(item) for O(n) vs. O(n*n) // ---------------------------------------------------------------------- for (child = doc.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() != Node.ELEMENT_NODE) { continue; } if (child.getNodeName().equalsIgnoreCase(META)) { metas.add(child); } else if (child.getNodeName().equalsIgnoreCase(HTML)) { html = child; } } // NodeList children = doc.getChildNodes(); // for(int i = 0; i < children.getLength(); i++) { // child = children.item(i); // if(child.getNodeType() != Node.ELEMENT_NODE) // continue; // if(child.getNodeName().equalsIgnoreCase(META)) // metas.add(child); // else if(child.getNodeName().equalsIgnoreCase(HTML)) // html = child; // } // check for META tags under HEAD if (html != null) { for (child = html.getFirstChild(); (child != null) && (head == null); child = child.getNextSibling()) { if (child.getNodeType() != Node.ELEMENT_NODE) { continue; } if (child.getNodeName().equalsIgnoreCase(HEAD)) { head = child; } } // children = html.getChildNodes(); // for(int i = 0; i < children.getLength() && head == null; i++) { // child = children.item(i); // if(child.getNodeType() != Node.ELEMENT_NODE) // continue; // if(child.getNodeName().equalsIgnoreCase(HEAD)) // head = child; // } } if (head != null) { for (head.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() != Node.ELEMENT_NODE) { continue; } if (child.getNodeName().equalsIgnoreCase(META)) { metas.add(child); } } // children = head.getChildNodes(); // for(int i = 0 ; i < children.getLength(); i++) { // child = children.item(i); // if(child.getNodeType() != Node.ELEMENT_NODE) // continue; // if(child.getNodeName().equalsIgnoreCase(META)) // metas.add(child); // } } return getMetaScriptType(metas); }
From source file:com.igormaznitsa.upom.UPomModel.java
private static Node nextSiblingElement(final Node node) { if (node == null) { return null; }//from ww w .ja v a 2 s. co m Node next = node.getNextSibling(); while (next != null && next.getNodeType() != Node.ELEMENT_NODE) { next = next.getNextSibling(); } return next; }
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. ja v a 2 s . c o m public static Element firstChildElement(Element element, String childElementName, String attrName, String attrValue) { 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; String value = childElement.getAttribute(attrName); if (value != null && value.equals(attrValue)) { return childElement; } } } while ((node = node.getNextSibling()) != null); } return null; }