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

/**
 * Insert the given {@code newElement} just next to the given {@code sibling}, after it.
 *
 * @param newElement/* w w  w  .ja  v  a 2 s  .c  o m*/
 * @param sibling
 */
public static void insertSiblingAfter(@Nonnull Element newElement, @Nonnull Element sibling) {
    Node nextSibling = sibling.getNextSibling();
    sibling.getParentNode().insertBefore(newElement, nextSibling);
}

From source file:Main.java

/**
 * Checks to see if parent node has more of these nodes looks at tagName
 * //w w  w  .  j av a  2s  .c o  m
 * @param node
 * @return
 */
private static final boolean parentNodeHasMoreOfThese(Element node) {
    int count = 0;
    NodeList children = node.getParentNode().getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getTagName().equals(((Element) child).getTagName())) {
                count++;
                if (count > 1)
                    return true;
            }
        }
    }

    return false;
}

From source file:Main.java

/**
 * Get the XML children element of an XML element, but only those of a
 * certain type/*from  w  w w .  j a va 2  s . c o  m*/
 *
 * @param parent
 *            The parent element to get the children from
 * @param elementTag
 *            The tag of the elements to return
 * @return The list of children {@link Element} of the parent
 */
public static List<Element> getChildElements(Element parent, String elementTag) {
    /* get the state providers and find the corresponding one */
    NodeList nodes = parent.getElementsByTagName(elementTag);
    List<Element> childElements = new ArrayList<>();

    for (int i = 0; i < nodes.getLength(); i++) {
        Element node = (Element) nodes.item(i);
        if (node.getParentNode().equals(parent)) {
            childElements.add(node);
        }
    }
    return childElements;
}

From source file:Main.java

public static final void setComment(Element element, String data) {
    Comment comm = element.getOwnerDocument().createComment(data != null ? data : ""); //$NON-NLS-1$
    element.getParentNode().insertBefore(comm, element);
}

From source file:Main.java

public static void insertLeadingPI(Document doc, String target, String data) {
    Element element = doc.getDocumentElement();
    ProcessingInstruction pi = doc.createProcessingInstruction(target, data);
    element.getParentNode().insertBefore(pi, element);
}

From source file:Main.java

public static void insertTrailingPI(Document doc, String target, String data) {
    Element element = doc.getDocumentElement();
    ProcessingInstruction pi = doc.createProcessingInstruction(target, data);
    element.getParentNode().appendChild(pi);
}

From source file:Utils.java

public static String getPrefixRecursive(Element el, String ns) {
    String prefix = getPrefix(el, ns);
    if (prefix == null && el.getParentNode() instanceof Element) {
        prefix = getPrefixRecursive((Element) el.getParentNode(), ns);
    }//from w  w  w  .  j  a  v  a  2  s.co m
    return prefix;
}

From source file:Main.java

public static NodeList findElements(Element parent, String fullXPath) {
    NodeList list = null;//  ww w  . j ava  2  s  .c  o  m
    Element elt = findSingleElement(parent, fullXPath);
    if (elt != null)
        list = ((Element) elt.getParentNode()).getElementsByTagName(elt.getNodeName());
    return list;
}

From source file:Main.java

public static void moveElement(Document fromDoc, Document toDoc, Element root, String elementName) {
    NodeList list = fromDoc.getElementsByTagName(elementName);
    if ((list != null) && (list.getLength() > 0)) {
        Element element = (Element) list.item(0);
        Node node = toDoc.importNode(element, true);
        root.appendChild(node);//from  www.  ja v a 2s .c  om
        element.getParentNode().removeChild(element);
    }
}

From source file:Main.java

public static Element getParentXElement(Element startElement, String xmlTag) {
    boolean found = false;
    if (startElement == null)
        return null;
    Node parentNode = startElement.getParentNode();
    while (!found) {
        if (parentNode == null)
            found = true;/*  w  ww  .j  av a  2s  . co  m*/
        else if (parentNode.getNodeName().equals(xmlTag))
            found = true;
        if (!found)
            parentNode = parentNode.getParentNode();
    }
    return (Element) parentNode;
}