List of usage examples for org.w3c.dom Node getParentNode
public Node getParentNode();
From source file:Main.java
/** * Method getStrFromNode/* ww w . ja va 2s . c o m*/ * * @param xpathnode * @return the string for the node. */ public static String getStrFromNode(Node xpathnode) { if (xpathnode.getNodeType() == Node.TEXT_NODE) { // we iterate over all siblings of the context node because eventually, // the text is "polluted" with pi's or comments StringBuilder sb = new StringBuilder(); for (Node currentSibling = xpathnode.getParentNode() .getFirstChild(); currentSibling != null; currentSibling = currentSibling.getNextSibling()) { if (currentSibling.getNodeType() == Node.TEXT_NODE) { sb.append(((Text) currentSibling).getData()); } } return sb.toString(); } else if (xpathnode.getNodeType() == Node.ATTRIBUTE_NODE) { return ((Attr) xpathnode).getNodeValue(); } else if (xpathnode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) { return ((ProcessingInstruction) xpathnode).getNodeValue(); } return null; }
From source file:com.photon.phresco.impl.WindowsApplicationProcessor.java
private static void updateContent(Document doc, List<ArtifactGroup> artifactGroups, List<Node> itemGroup, String elementName) {// w w w . j a v a 2s .co m for (Node node : itemGroup) { NodeList childNodes = node.getChildNodes(); for (int j = 0; j < childNodes.getLength(); j++) { Node item = childNodes.item(j); if (item.getNodeName().equals(elementName)) { Node parentNode = item.getParentNode(); for (ArtifactGroup artifactGroup : artifactGroups) { if (artifactGroup.getType().name().equals(Type.FEATURE.name())) { Element content = doc.createElement(elementName); if (elementName.equalsIgnoreCase(REFERENCE)) { content.setAttribute(INCLUDE, artifactGroup.getName() + DLL); Element hintPath = doc.createElement(HINTPATH); hintPath.setTextContent( DOUBLE_DOT + COMMON + File.separator + artifactGroup.getName() + DLL); content.appendChild(hintPath); } else { content.setAttribute(INCLUDE, artifactGroup.getName() + DLL); } parentNode.appendChild(content); } } break; } } } }
From source file:Main.java
/** * get the node following the node in parameter * /* w w w . j av a 2 s. c o m*/ * @param n * the n * @return the node */ public static Node next(Node n) { if (n == null) { return null; } Node currentNode = n; if (currentNode.getChildNodes().getLength() > 0) { currentNode = currentNode.getFirstChild(); } else { if (currentNode.getNextSibling() != null) { currentNode = currentNode.getNextSibling(); } else { Node oldCurrentNode = currentNode; currentNode = currentNode.getParentNode().getNextSibling(); while (oldCurrentNode != null && currentNode == null) { oldCurrentNode = oldCurrentNode.getParentNode(); if (oldCurrentNode != null) { currentNode = oldCurrentNode.getNextSibling(); } } } } return currentNode; }
From source file:Main.java
public static boolean isSpacePreserved(Node node) { if (node == null) { return false; }/*from ww w . ja v a2 s. c o m*/ if (node.getNodeType() == Node.ELEMENT_NODE) { final String spaceAttr = ((Element) node).getAttributeNS(XMLConstants.XML_NS_URI, "space"); if (spaceAttr != null) { return ("preserve".equals(spaceAttr)); } } return isSpacePreserved(node.getParentNode()); }
From source file:Main.java
/** * Constructs a XPath query to the supplied node. * //from w w w . ja v a 2 s. co m * @param n * @return */ public static String getXPath(Node n) { if (null == n) { throw new IllegalArgumentException("Invalid node"); } ArrayList<Node> hierarchy = new ArrayList<Node>(); StringBuffer buffer = new StringBuffer(); Node parent = null; // Push parent element's on stack hierarchy.add(n); parent = n.getParentNode(); while (parent != null && parent.getNodeType() != Node.DOCUMENT_NODE) { hierarchy.add(0, parent); parent = parent.getParentNode(); } Iterator<Node> i = hierarchy.iterator(); while (i.hasNext()) { Node node = i.next(); buffer.append("/"); buffer.append(node.getNodeName()); if (node.hasAttributes()) { Node uuid = node.getAttributes().getNamedItem("uuid"); if (uuid != null) { buffer.append("[@uuid='"); buffer.append(uuid.getNodeValue()); buffer.append("']"); } } } // return buffer return buffer.toString(); }
From source file:Main.java
/** * Returns true if the descendantOrSelf is on the descendant-or-self axis * of the context node./*from w w w.java 2 s . c o m*/ * * @param ctx * @param descendantOrSelf * @return true if the node is descendant */ static public boolean isDescendantOrSelf(Node ctx, Node descendantOrSelf) { if (ctx == descendantOrSelf) { return true; } Node parent = descendantOrSelf; while (true) { if (parent == null) { return false; } if (parent == ctx) { return true; } if (parent.getNodeType() == Node.ATTRIBUTE_NODE) { parent = ((Attr) parent).getOwnerElement(); } else { parent = parent.getParentNode(); } } }
From source file:Main.java
public static String pathUp(Node node, int level) { StringBuffer buf = new StringBuffer(); int current = level; while (node != null && current > 0) { if (node instanceof Element) { if (buf.length() > 0) { buf.insert(0, "/"); }/*w ww.j a v a 2s . co m*/ buf.insert(0, node.getNodeName()); current = current - 1; } node = node.getParentNode(); } return buf.toString(); }
From source file:Main.java
/** * Returns true if the descendantOrSelf is on the descendant-or-self axis * of the context node./* w w w . j a va 2 s . com*/ * * @param ctx * @param descendantOrSelf * @return true if the node is descendant */ public static boolean isDescendantOrSelf(Node ctx, Node descendantOrSelf) { if (ctx == descendantOrSelf) { return true; } Node parent = descendantOrSelf; while (true) { if (parent == null) { return false; } if (parent == ctx) { return true; } if (parent.getNodeType() == Node.ATTRIBUTE_NODE) { parent = ((Attr) parent).getOwnerElement(); } else { parent = parent.getParentNode(); } } }
From source file:Main.java
private static String getValueByTagName(Node n, String tag) { if (n == null) return null; if (tag.equals(n.getLocalName())) return n.getFirstChild().getNodeValue(); if (n.hasChildNodes()) return getValueByTagName(n.getFirstChild(), tag); else if (n.getNextSibling() != null) return getValueByTagName(n.getNextSibling(), tag); else// w w w. ja va 2 s. c om return getValueByTagName(n.getParentNode().getNextSibling(), tag); }
From source file:Main.java
public static String getNamespaceURI(final org.w3c.dom.Node n, final String prefix) { Node prefixDeclaration = n.getAttributes().getNamedItem("xmlns:" + prefix); if (prefixDeclaration != null) { // we have found the good NameSpace return prefixDeclaration.getNodeValue(); }/*from w w w . j a v a 2 s. c om*/ // we have found the good NameSpace // we look for the NameSpace in the parent Node return getNamespaceURI(n.getParentNode(), prefix); }