List of usage examples for org.w3c.dom Node getParentNode
public Node getParentNode();
From source file:Main.java
public static void removeAll(Node node, short nodeType, String name) { if (node.getNodeType() == nodeType && (name == null || node.getNodeName().equals(name))) { node.getParentNode().removeChild(node); } else {/*from w w w. j av a2 s . c o m*/ NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { removeAll(list.item(i), nodeType, name); } } }
From source file:Main.java
public static boolean isDescendant(Node testNode, Node compareToNode) { if (testNode != null && compareToNode != null) { while (testNode.getParentNode() != null) { if (testNode.getParentNode() == compareToNode) { return true; }/* w ww. j a v a 2 s .c o m*/ testNode = testNode.getParentNode(); } } return false; }
From source file:Main.java
@SuppressWarnings("UseSpecificCatch") public static boolean saveXML(Document doc, File outfile, boolean indent) { try {//from w ww .j ava 2 s.c o m Transformer transformer = TransformerFactory.newInstance().newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new OutputStreamWriter(new FileOutputStream(outfile), "UTF-8")); if (indent) { XPathFactory xpathFactory = XPathFactory.newInstance(); // XPath to find empty text nodes. XPathExpression xpathExp = xpathFactory.newXPath().compile("//text()[normalize-space(.) = '']"); NodeList emptyTextNodes = (NodeList) xpathExp.evaluate(doc, XPathConstants.NODESET); // Remove each empty text node from document. for (int i = 0; i < emptyTextNodes.getLength(); i++) { Node emptyTextNode = emptyTextNodes.item(i); emptyTextNode.getParentNode().removeChild(emptyTextNode); } transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); } transformer.transform(source, result); return true; } catch (Exception ex) { return false; } }
From source file:Main.java
public static String[] getElementPath(Node node) { Vector vector = new Vector(); for (; node != null; node = node.getParentNode()) if (node.getNodeType() == 1) if (!hasSameNamedSibling(node)) vector.addElement(node.getNodeName()); else//from w w w . ja va2s .c om vector.addElement(node.getNodeName() + "[" + (getElementIndex(node) + 1) + "]"); int i = vector.size(); String as[] = new String[i]; int j = i - 1; for (int k = 0; k < i;) { as[k] = (String) vector.elementAt(j); k++; j--; } return as; }
From source file:Main.java
public static List elements(Element element) { List elements = new ArrayList(); NodeList nodeList = element.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if ((node instanceof Element) && (element == node.getParentNode())) { elements.add(node);/*from www . ja v a 2 s . co m*/ } } return elements; }
From source file:Main.java
public static boolean hasParent(Node e, String ns, String name) { if (e == null) return false; return isA(e.getParentNode(), ns, name); }
From source file:Main.java
/** * in this NQF project, the root node is QualityMeasureDocument * @param aNode/* w w w . j a v a 2 s . c o m*/ * @param rootNode * @return the distance a node to the rootnode */ public static int getHeight(Node aNode, Node rootNode) { int height = 0; if (aNode == null) { return 0; } while (aNode.getParentNode() != rootNode) { aNode = aNode.getParentNode(); height++; } return height; }
From source file:Main.java
/** * Gets the immediately child elements list from the parent element. * /*from w w w . j av a2 s .c om*/ * @param parent the parent element in the element tree * @param tagName the specified tag name * @return the NOT NULL immediately child elements list */ public static List<Element> getChildElements(Element parent, String tagName) { NodeList nodes = parent.getElementsByTagName(tagName); List<Element> elements = new ArrayList<Element>(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node instanceof Element && node.getParentNode() == parent) { elements.add((Element) node); } } return elements; }
From source file:Main.java
/** * Gets the immediately child elements list from the parent element. * /*from ww w . j a va 2 s . c o m*/ * @param parent the parent element in the element tree * @return the NOT NULL immediately child elements list */ public static List<Element> getChildElements(Element parent) { NodeList nodes = parent.getChildNodes(); List<Element> elements = new ArrayList<Element>(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node instanceof Element && node.getParentNode() == parent) { elements.add((Element) node); } } return elements; }
From source file:Main.java
public static final String indenting(Node rootNode) throws XPathExpressionException, TransformerException { XPath xPath = XPathFactory.newInstance().newXPath(); NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", rootNode, XPathConstants.NODESET); for (int i = 0; i < nodeList.getLength(); ++i) { Node node = nodeList.item(i); node.getParentNode().removeChild(node); }//from w ww .j a v a 2 s. com Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); StringWriter stringWriter = new StringWriter(); StreamResult streamResult = new StreamResult(stringWriter); transformer.transform(new DOMSource(rootNode), streamResult); return stringWriter.toString(); }