List of usage examples for org.w3c.dom Element getParentNode
public Node getParentNode();
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);//from w w w . j a v a2 s . com factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); Element element = doc.getDocumentElement(); Element element2 = doc.createElement("newname"); NamedNodeMap attrs = element.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Attr attr2 = (Attr) doc.importNode(attrs.item(i), true); element2.getAttributes().setNamedItem(attr2); } while (element.hasChildNodes()) { element2.appendChild(element.getFirstChild()); } element.getParentNode().replaceChild(element2, element); }
From source file:Main.java
public static void removeElement(Element element) { if (element.getParentNode() != null) { element.getParentNode().removeChild(element); }/*from w w w . java 2s . c o m*/ }
From source file:Main.java
public static Element getParent(Element elem) { Node parent = elem.getParentNode(); if (parent instanceof Element) return (Element) parent; return null;//from www . j ava 2s . co m }
From source file:Main.java
/** * Method to get the depth of a XML Element. * @param element the XML Element.//from w w w . java 2 s . c om * @return the int depth of the XML Element. */ public static int getDepth(Element element) { Node parent = element.getParentNode(); int depth = 0; while (parent != null && parent.getNodeType() == Node.ELEMENT_NODE) { depth++; parent = parent.getParentNode(); } return depth; }
From source file:Main.java
/** * Find the root element of the given node. * * @param child The xml element to serach for the root of. * @return The root of the DOM tree that the given child exists in. *///from ww w. jav a 2 s . c o m public static Element findRoot(Element child) { Node parent = child.getParentNode(); while ((parent != null) && (parent instanceof Element)) { child = (Element) parent; parent = child.getParentNode(); } return child; }
From source file:Main.java
/** * Remove the Pi on top of e// ww w . j ava2 s. c o m * * @param e the current element * @param pi the pi element */ public static void removeTopPi(Element e, Element pi) { Element piContainer = (Element) pi.getParentNode(); piContainer.replaceChild(e, pi); }
From source file:Main.java
public static void deleteElement(Document fromDoc, String elementName) { NodeList list = fromDoc.getElementsByTagName(elementName); if ((list != null) && (list.getLength() > 0)) { Element element = (Element) list.item(0); element.getParentNode().removeChild(element); }// w w w.j a v a 2 s . com }
From source file:Main.java
/** * Get parent element of specified element *///from w w w. ja v a 2 s . c o m public static Element getParentElement(Element element) { Node parent = element.getParentNode(); while (parent != null) { if (parent instanceof Element) return (Element) parent; parent = parent.getParentNode(); } return null; }
From source file:Main.java
/** * /*from w ww . j ava 2 s . co m*/ * @param dataRoot * the starting node * @param name * the name of the subelement to find * @return the list of all DOM Element with the provided name direct child * of the starting node */ public static List<Element> getElementList(Element dataRoot, String name) { NodeList list = dataRoot.getElementsByTagName(name); List<Element> listElements = new ArrayList<Element>(); for (int i = 0; i < list.getLength(); i++) { Element item = (Element) list.item(i); if (item.getParentNode().equals(dataRoot)) { listElements.add(item); } } return listElements; }
From source file:Main.java
/** * Append XPath [expr] like /a/b[1] or /a/b[@name='x'] * @param sb// www . j a va 2 s.c o m * @param n */ private static void appendElementQualifier(StringBuffer sb, Element n) { if (n.getParentNode() == n.getOwnerDocument()) { return; } if (n.getAttributes() != null && n.getAttributes().getNamedItem("name") != null) { sb.append("[@name='").append(n.getAttributes().getNamedItem("name").getNodeValue()).append("']"); } else if (getChildrenCount((Element) n.getParentNode(), n.getNamespaceURI(), n.getLocalName()) != 1) { sb.append("[").append(getPosition(n)).append("]"); } }