Example usage for org.w3c.dom Element getParentNode

List of usage examples for org.w3c.dom Element getParentNode

Introduction

In this page you can find the example usage for org.w3c.dom Element getParentNode.

Prototype

public Node getParentNode();

Source Link

Document

The parent of this node.

Usage

From source file:Main.java

/**
 * Returns a set of the direct ancestors of each element in elements.
 * If one element does not have an ancestor, the whole set will be empty.
 *//*from  www. j a v a 2 s.c  om*/
public static Set<Element> getDirectAncestors(Set<Element> elements) {
    Set<Element> directAncestors = new HashSet<Element>();
    for (Element element : elements) {
        if (element.getParentNode() != null && element.getParentNode() instanceof Element) {
            directAncestors.add((Element) element.getParentNode());
        } else {
            return new HashSet<Element>();
        }
    }
    return directAncestors;
}

From source file:Main.java

/**
 * Method to delete an element based on tag name .
 * @param doc/*from w  w w. j a  v a 2 s .  c  o m*/
 * @param tagName
 * @return
 */
private static Document deleteElement(Document doc, String tagName) {
    //only 2 of 3 were deleted ?
    NodeList nodes = doc.getElementsByTagName(tagName);
    for (int i = 0; i < nodes.getLength(); i++) {
        Element e = (Element) nodes.item(i);
        e.getParentNode().removeChild(e);
    }
    return doc;
}

From source file:Main.java

private static List<Element> getDirectChildren(Element element, String tagName) {
    NodeList nList = element.getElementsByTagName(tagName);
    List<Element> list = new ArrayList<Element>();
    int listLength = nList.getLength();
    for (int i = 0; i < listLength; i++) {
        Element listElement = (Element) nList.item(i);
        if (listElement.getParentNode() == element) {
            list.add(listElement);/*from   w w  w .j  a va  2 s  .  c  o m*/
        }
    }
    return list;
}

From source file:Main.java

public static void insertSiblingAfter(Element newElement, Element sibling) {
    Node nextSibling = sibling.getNextSibling();
    sibling.getParentNode().insertBefore(newElement, nextSibling);
}

From source file:Main.java

/**
 * @param el an Element/*from   w  w  w  .j  a v  a  2s.  co  m*/
 * @return the ordinal position 1..N of this element amongst its sibling
 * elements (of any name), as a String; 
 * or '0' if the element has no parent
 */
public static String ordinalPosition(Element el) {
    String position = "0";
    Node parent = el.getParentNode();
    if (parent instanceof Element) {
        int pos = 0;
        NodeList nl = ((Element) parent).getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node n = nl.item(i);
            if (n instanceof Element)
                pos++;
            if (n.equals(el))
                position = new Integer(pos).toString();
        }
    }
    return position;
}

From source file:Main.java

public static Element getElement(Element element, String tagName) {
    final NodeList nodeList = element.getElementsByTagName(tagName);
    if (nodeList == null || nodeList.getLength() < 1) {
        return null;
    }//from  ww w . jav  a2s.  c  o m
    int length = nodeList.getLength();
    for (int i = 0; i < length; i++) {
        Element childEle = (Element) nodeList.item(i);
        if (childEle == null || childEle.getParentNode() == element) {
            return childEle;
        }
    }
    return null;
}

From source file:Main.java

public static void edit(Document doc) {
    NodeList list = doc.getElementsByTagName("name");
    Element element = (Element) list.item(0);

    Element dup = (Element) element.cloneNode(true);

    element.getParentNode().insertBefore(dup, element.getNextSibling());
}

From source file:Main.java

/**
 * Corresponds to the pos() xpath function
 * /*from  w w w.  j a  v  a 2  s .co  m*/
 * @param e
 * @return pos(), also the start index is 1 not 0
 */
public static int getPosition(Element e) {
    Element parent = (Element) e.getParentNode();
    List<Element> children = getChildElements(parent);
    return children.indexOf(e) + 1;
}

From source file:Main.java

public static List<Element> transElements(Element parentEle, NodeList nodeList) {
    final ArrayList<Element> elements = new ArrayList<Element>();
    int length = nodeList.getLength();
    for (int i = 0; i < length; i++) {
        Element element = (Element) nodeList.item(i);
        if (parentEle == null || element.getParentNode() == parentEle) {
            elements.add(element);//from   ww w  .ja  v a2  s . com
        }
    }

    return elements;
}

From source file:Main.java

static public Element selectParentElement(Element element, String parentName) {
    Element parentElement = (Element) element.getParentNode();
    if (parentElement == null) {
        return null;
    }/*ww  w  .  j av a 2 s .co m*/
    if (parentElement.getTagName().equals(parentName)) {
        return parentElement;
    }
    return selectParentElement(parentElement, parentName);
}