List of usage examples for org.w3c.dom Node getPreviousSibling
public Node getPreviousSibling();
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);//from ww w. j ava2 s.com factory.setExpandEntityReferences(false); Document doc1 = factory.newDocumentBuilder().parse(new File("filename")); NodeList list = doc1.getElementsByTagName("entry"); Element element = (Element) list.item(0); Document doc2 = factory.newDocumentBuilder().parse(new File("infilename2.xml")); // Make a copy of the element subtree suitable for inserting into doc2 Node node = doc2.importNode(element, true); // Get the parent Node parent = node.getParentNode(); // Get children NodeList children = node.getChildNodes(); // Get first child; null if no children Node child = node.getFirstChild(); // Get last child; null if no children child = node.getLastChild(); // Get next sibling; null if node is last child Node sibling = node.getNextSibling(); // Get previous sibling; null if node is first child sibling = node.getPreviousSibling(); // Get first sibling sibling = node.getParentNode().getFirstChild(); // Get last sibling sibling = node.getParentNode().getLastChild(); }
From source file:Main.java
/** * Gets the previous comment./* w ww .java 2 s . co m*/ * * @param element * the element * @return the previous comment */ public static String getPreviousComment(Node element) { while (element.getPreviousSibling() != null) { Node prev = element.getPreviousSibling(); if (prev.getNodeType() == Node.COMMENT_NODE) { return prev.getTextContent(); } else if (prev.getNodeType() == Node.TEXT_NODE) { return getPreviousComment(prev); } else if (prev.getNodeType() == Node.ELEMENT_NODE) { return null; } } return null; }
From source file:Main.java
public static Element previousSiblingElement(Node node) { for (Node tempNode = node.getPreviousSibling(); tempNode != null; tempNode = tempNode .getPreviousSibling()) {/*from www . j av a 2s. c o m*/ if (tempNode.getNodeType() == Node.ELEMENT_NODE) { return (Element) tempNode; } } return null; }
From source file:Main.java
private static Node getPreviousTypedNode(Node node, short nodeType) { node = node.getPreviousSibling(); while (node != null && node.getNodeType() != nodeType) { node = node.getPreviousSibling(); }//from ww w .j a va 2 s.c o m return node; }
From source file:Main.java
/** * aims at finding the location of the current node of its kind. * the idea is to count how many previous siblings does it have, * @param aNode//w ww . j a v a 2s . c o m * @return */ public static int getLocHomoSibling(Node aNode) { int countOfBranch = 0; Node preSibling = aNode; while ((preSibling = preSibling.getPreviousSibling()) != null) { if (preSibling.getNodeName().equals(aNode.getNodeName())) { countOfBranch++; } } return countOfBranch; }
From source file:Main.java
public static boolean hasOnlyTextSiblings(/*@Nonnull*/Node node) { Node leftSibling = node.getPreviousSibling(); while (leftSibling != null) { if (!(leftSibling instanceof Text)) { return false; }/*from www .ja v a2s .c o m*/ leftSibling = leftSibling.getPreviousSibling(); } Node rightSibling = node.getNextSibling(); while (rightSibling != null) { if (!(rightSibling instanceof Text)) { return false; } rightSibling = rightSibling.getNextSibling(); } return true; }
From source file:Main.java
public static boolean hasOnlyTextSiblings(@Nonnull Node node) { Node leftSibling = node.getPreviousSibling(); while (leftSibling != null) { if (!(leftSibling instanceof Text)) { return false; }/* w w w . j a v a 2 s .c o m*/ leftSibling = leftSibling.getPreviousSibling(); } Node rightSibling = node.getNextSibling(); while (rightSibling != null) { if (!(rightSibling instanceof Text)) { return false; } rightSibling = rightSibling.getNextSibling(); } return true; }
From source file:Main.java
public static boolean hasSameNamedSibling(Node node) { String s = node.getNodeName(); for (Node node1 = node.getPreviousSibling(); node1 != null; node1 = node1.getPreviousSibling()) if (node1.getNodeName().equals(s) && node1.getNodeType() == node.getNodeType()) return true; for (Node node2 = node.getNextSibling(); node2 != null; node2 = node2.getNextSibling()) if (node2.getNodeName().equals(s) && node2.getNodeType() == node.getNodeType()) return true; return false; }
From source file:Main.java
/** * Returns the index of a node ./* w ww . ja va 2 s . c om*/ * * @param elt * @return */ public static final int getElementIdx(Node elt) { int count = 0; for (Node sib = elt.getPreviousSibling(); sib != null; sib = sib.getPreviousSibling()) { if (sib.getNodeType() == Node.ELEMENT_NODE && ((Element) sib).getTagName().equals(((Element) elt).getTagName())) count++; } return count; }
From source file:Main.java
public static Node getPrevious(Node current, String name, int type) { Node prev = current.getPreviousSibling(); if (prev == null) return null; for (Node node = prev; node != null; node = node.getPreviousSibling()) { if (type >= 0 && node.getNodeType() != type) { continue; } else {// w w w.j ava 2 s . c o m if (name == null) return node; if (name.equals(node.getNodeName())) { return node; } } } return null; }