List of usage examples for org.w3c.dom Node getNextSibling
public Node getNextSibling();
From source file:de.mpg.imeji.presentation.metadata.extractors.BasicExtractor.java
static void displayMetadata(List<String> techMd, Node node, int level) { StringBuffer sb = new StringBuffer(); // print open tag of element indent(techMd, sb, level);/*from ww w . ja va2 s.c om*/ sb.append("<" + node.getNodeName()); NamedNodeMap map = node.getAttributes(); if (map != null) { // print attribute values int length = map.getLength(); for (int i = 0; i < length; i++) { Node attr = map.item(i); sb.append(" " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\""); } } Node child = node.getFirstChild(); if (child == null) { // no children, so close element and return sb.append("/>"); techMd.add(sb.toString()); sb.delete(0, sb.length()); return; } // children, so close current tag sb.append(">"); techMd.add(sb.toString()); sb.delete(0, sb.length()); while (child != null) { // print children recursively displayMetadata(techMd, child, level + 1); child = child.getNextSibling(); } // print close tag of element indent(techMd, sb, level); sb.append("</" + node.getNodeName() + ">"); techMd.add(sb.toString()); sb.delete(0, sb.length()); }
From source file:com.igormaznitsa.upom.UPomModel.java
private static Node findFirstElement(final Node node) { if (node == null) { return null; }/* ww w .j a v a 2 s . c o m*/ Node result = node.getFirstChild(); while (result != null && result.getNodeType() != Node.ELEMENT_NODE) { result = result.getNextSibling(); } return result; }
From source file:Main.java
/** * Get the text that is associated with this element. * //from w w w .j a v a2 s .co m * @param element an Element object. * @return the text that is associated with this element. */ public static String getText(Element element) { String text = null; // Get first child element Node node = element.getFirstChild(); // NodeList nodeList = element.getChildNodes(); // int length = nodeList.getLength(); // Process while there are nodes and the text hasn't been found while ((node != null) && (text == null)) { // If a text node or cdata section is found, then get text if ((node.getNodeType() == Node.TEXT_NODE) || (node.getNodeType() == Node.CDATA_SECTION_NODE)) { text = ((CharacterData) node).getData(); } // Get next sibling node = node.getNextSibling(); } if (text != null) text = text.trim(); return text; }
From source file:importer.handler.post.stages.Discriminator.java
/** * Get the next sibling that is an element * @param elem the element//from w ww .j a v a 2 s.co m * @param skipText if true skip text nodes to next element node * @return its next sibling of elem or null */ static Element nextSibling(Element elem, boolean skipText) { Node n = elem.getNextSibling(); while (n != null) { if (!skipText && n.getNodeType() == Node.TEXT_NODE && !isWhitespace(n.getTextContent())) return null; else if (n.getNodeType() == Node.ELEMENT_NODE) return (Element) n; n = n.getNextSibling(); } return null; }
From source file:Main.java
/** * Gets Node Index value for the XPath expression<br> * <p/>//from w ww. j a va 2s. c o m * e.g. <root><a><b>ritesh</b><b>trivedi</b></a></root> calling * <p/> * getXPathNodeIndex for Node with value * <p/> * trivedi would return 2 */ public static int getXPathNodeIndex(Node node, boolean ignoreWhitespace) { int nodeIndex = 0; if (node == null) { return -1; //throw new IllegalArgumentException("Node argument for getXPathNodeIndex cannot be null"); } Node prevNode = node; //log("getXPathNodeIndex info next few lines"); //log("Current node:"); //printNode(node); while ((prevNode = prevNode.getPreviousSibling()) != null) { //log("previous node"); //printNode(prevNode); if (nodesEqual(node, prevNode, ignoreWhitespace)) nodeIndex++; } // If similar children are found, ONLY then increase // the nodeIndex by 1 since XPath exprn starts at 1 and not 0 if (nodeIndex > 0) nodeIndex++; if (nodeIndex == 0) { Node nextNode = node; boolean found = false; while (((nextNode = nextNode.getNextSibling()) != null) && (!found)) { //log("Next node"); //printNode(nextNode); if (nodesEqual(node, nextNode, ignoreWhitespace)) { nodeIndex++; found = true; } //node = prevNode; } } return nodeIndex; }
From source file:XMLUtils.java
/** * Get the next sibling element of a given element. * @param el//from ww w.ja v a 2 s.c o m * @return */ public static Element getNext(Element el) { Node n = el.getNextSibling(); while (n != null && !(n instanceof Element)) { // get the next one n = n.getNextSibling(); } if (n instanceof Element) { return (Element) n; } // else, nothing to return return null; }
From source file:Main.java
/** * @param sibling// w w w.j a va2 s.c o m * @param uri * @param nodeName * @return nodes with the constrain */ public static Element[] selectNodes(Node sibling, String uri, String nodeName) { int size = 20; Element[] a = new Element[size]; int curr = 0; //List list=new ArrayList(); while (sibling != null) { if (nodeName.equals(sibling.getLocalName()) && uri.equals(sibling.getNamespaceURI())) { a[curr++] = (Element) sibling; if (size <= curr) { int cursize = size << 2; Element[] cp = new Element[cursize]; System.arraycopy(a, 0, cp, 0, size); a = cp; size = cursize; } } sibling = sibling.getNextSibling(); } Element[] af = new Element[curr]; System.arraycopy(a, 0, af, 0, curr); return af; }
From source file:DOMUtils.java
/** * Concat all the text and cdata node children of this elem and return * the resulting text./*from ww w. j a v a 2 s .c om*/ * * @param parentEl the element whose cdata/text node values are to * be combined. * @return the concatanated string. */ 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; } tempNode = tempNode.getNextSibling(); } return strBuf.toString(); }
From source file:Main.java
/** * Get the text content of an element identified by a path, * where the path elements can include an index. The first * path element must not have an index, and its name must * match the name of the starting node. If the starting * node is a Document, the root Element of the document is * used as the starting point. Path elements must be separated * by the slash character. If the path starts with a slash, * the slash is ignored. If the element or attribute identified * by the path is not present as a child of the starting node, * the empty string is returned. If a path element identifies * an attribute, any subsequent path elements are ignored. * A path is in the form: /e1/e2/e3/... or /e1/e2/@attr * Note the slash preceding the attribute's @-sign. * @param node the starting node for the path. The first path * element must match the name of this node. * @param path the path to the target node. * @return the full text value of the target node (including all * descendent text nodes), or the empty string if the target is * not a descendent of the starting node. *//*from w ww . j av a 2 s. c o m*/ public static String getTextContent(Node node, String path) { if (node instanceof Document) node = ((Document) node).getDocumentElement(); if (!(node instanceof Element)) return ""; Element el = (Element) node; path = path.replaceAll("\\s", ""); if (path.startsWith("/")) path = path.substring(1); String[] pathElements = path.split("/"); if (!pathElements[0].equals(el.getTagName())) return ""; for (int i = 1; i < pathElements.length; i++) { String pe = pathElements[i]; if (pe.startsWith("@")) { //If this path element identifies an attribute, return it //and ignore any further path elements. return el.getAttribute(pe.substring(1)); } else { //This path element identifies an Element. It may have an index. //Get the index, if present, and get the element name. int n = 0; int k = pe.indexOf("["); int kk = pe.lastIndexOf("]"); if ((k != -1) && (k < kk)) { try { n = Integer.parseInt(pe.substring(k + 1, kk)); } catch (Exception ex) { return ""; } pe = pe.substring(0, k); } else if (k != kk) return ""; //We now have the element name and the index. //Find the identified Element. We have to count //matching elements to find the one identified //by the index. int nn = 0; Node child = el.getFirstChild(); while (child != null) { if ((child.getNodeType() == Node.ELEMENT_NODE) && child.getNodeName().equals(pe)) { if (n == nn) break; nn++; } child = child.getNextSibling(); } //If the child is null, we didn't find the identified Element. if (child == null) return ""; //If we get here, we found it, now look for the next one. el = (Element) child; } } //Okay, we must be at the end of the path, and it must be an Element. //Return the text content of the element. return el.getTextContent(); }
From source file:ApplyXPathJAXP.java
static void printNodeList(NodeList nodelist) throws Exception { Node n; // Set up an identity transformer to use as serializer. Transformer serializer = TransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); for (int i = 0; i < nodelist.getLength(); i++) { n = nodelist.item(i);//from w w w. j av a 2s . c o m if (isTextNode(n)) { // DOM may have more than one node corresponding to a // single XPath text node. Coalesce all contiguous text nodes // at this level StringBuffer sb = new StringBuffer(n.getNodeValue()); for (Node nn = n.getNextSibling(); isTextNode(nn); nn = nn.getNextSibling()) { sb.append(nn.getNodeValue()); } System.out.print(sb); } else { serializer.transform(new DOMSource(n), new StreamResult(new OutputStreamWriter(System.out))); } System.out.println(); } }