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

public static Element addElementBefore(Document doc, Element sibling, String nodeName) {
    Element el = null;/*from   www  .j  av a 2 s .co m*/
    if (doc != null)
        el = doc.createElement(nodeName);

    if (sibling != null)
        sibling.getParentNode().insertBefore(el, sibling);

    return el;
}

From source file:Main.java

/**
 * Gets the parent element./*from  www. j a  va2s.c o  m*/
 * 
 * @param element
 *            the element
 * @return the parent element
 */
public static Element getParentElement(Element element) {
    if (element == null) {
        return null;
    }
    Element parentElement = null;
    Node parentNode = element.getParentNode();
    while (parentNode != null && parentElement == null) {
        if (parentNode.getNodeType() == Node.ELEMENT_NODE) {
            parentElement = (Element) parentNode;
        }
        if (parentNode.getNodeType() == Node.DOCUMENT_NODE) {
            parentElement = ((Document) parentNode).getDocumentElement();
            if (element.isSameNode(parentElement)) {
                parentElement = null;
            }
        }
        parentNode = parentNode.getParentNode();
    }

    return parentElement;
}

From source file:Main.java

public static String getXPath(Element elt) {
    String xpath = "";
    if (elt != null) {
        Document doc = elt.getOwnerDocument();
        Element root = doc.getDocumentElement();
        Element parent = elt;
        while (parent != root) {
            xpath = "/" + parent.getNodeName() + xpath;
            parent = (Element) parent.getParentNode();
        }/*from   ww w.ja  v a  2s  .c o m*/
    }
    return xpath;
}

From source file:Utils.java

/**
 * Get all prefixes defined, up to the root, for a namespace URI.
 * //from   w  w w .  j av  a2 s . c om
 * @param element
 * @param namespaceUri
 * @param prefixes
 */
public static void getPrefixesRecursive(Element element, String namespaceUri, List<String> prefixes) {
    getPrefixes(element, namespaceUri, prefixes);
    Node parent = element.getParentNode();
    if (parent instanceof Element) {
        getPrefixesRecursive((Element) parent, namespaceUri, prefixes);
    }
}

From source file:Main.java

public static void fixSize(final Element element) {
    final int len = element.getChildNodes().getLength();

    if (len > 0) {
        element.setAttribute("size", "" + len);
    } else {//ww  w  .j a va  2  s  .  c  om
        element.getParentNode().removeChild(element);
    }
}

From source file:Main.java

@SuppressWarnings({ "ChainOfInstanceofChecks" })
private static void formatElementStart(/*@Nonnull*/Document document, /*@Nonnull*/Element element, /*@Nonnull*/
        String formatString) {/*from   ww w .j  a v a2  s .  co m*/
    Node previousSibling = element.getPreviousSibling();

    if (previousSibling == null || previousSibling instanceof Element) {
        element.getParentNode().insertBefore(document.createTextNode(formatString), element);
    } else if (previousSibling instanceof Text) {
        Text textNode = (Text) previousSibling;
        String text = textNode.getWholeText();

        if (!formatString.equals(text)) {
            textNode.replaceWholeText(trimRight(text) + formatString);
        }
    }
}

From source file:Main.java

/**
 * @param element The element whose ancestry we will check
 * @param tagName The tagName of the element we are searching for in the ancestry
 * @param limitTagName Stop searching if we hit this limit
 * @return The first matching element, if found
 */// w  w w. j  a  v  a2s . c om
public static Element getAncestorOrSelf(final Element element, final String tagName,
        final String limitTagName) {
    Element result = null;
    Element next = element;
    String currentTagName;
    do {
        currentTagName = next.getTagName();
        if (currentTagName.equals(tagName)) {
            result = next;
            break;
        } else {
            Node parent = next.getParentNode();
            if (parent != null && parent.getNodeType() == Node.ELEMENT_NODE) {
                next = (Element) parent;
            } else {
                break;
            }
        }
    } while (next != null && (limitTagName == null || !tagName.equals(limitTagName)));
    return result;
}

From source file:Main.java

/**
 * Returns parent attribute value for a specific element (2 levels of
 * parents)/* w  w  w .j  a  va 2  s .  c om*/
 * @param element
 * @param attr
 * @return
 */
protected static String getXsdParentsAtrValue(Element element, String attr) {
    Node tmpElement = null;
    String tmpRes = "";
    try {
        for (int i = 1; i < 3; i++) {
            if (i == 1) {
                tmpElement = element.getParentNode();
            } else {
                tmpElement = tmpElement.getParentNode();
            }
            if (tmpElement.getNodeName().equals("xs:schema")) {
                if (i == 1) {
                    tmpRes = "..";
                } else {
                    tmpRes = "." + tmpRes;
                }
                break;
            }
            while ((tmpElement.getNodeType() != Node.ELEMENT_NODE)
                    || ((tmpElement.getNodeType() != Node.DOCUMENT_NODE)
                            && !((Element) tmpElement).hasAttribute(attr))) {
                if (tmpElement.getNodeName().equals("xs:schema")) {
                    break;
                }
                tmpElement = tmpElement.getParentNode();
            }
            tmpRes = ((Element) tmpElement).getAttribute(attr) + "." + tmpRes;
        }
        return tmpRes;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Utils.java

/**
 * Recursive method to find a given attribute value
 *//*from w  w  w.  j a  v a 2  s  .co m*/
public static String recursiveGetAttributeValue(Element element, String attributeName) {
    String answer = null;
    try {
        answer = element.getAttribute(attributeName);
    } catch (Exception e) {

    }
    if (answer == null || answer.length() == 0) {
        Node parentNode = element.getParentNode();
        if (parentNode instanceof Element) {
            return recursiveGetAttributeValue((Element) parentNode, attributeName);
        }
    }
    return answer;
}

From source file:Main.java

private static int getLocationInParent(Element expectedChild, String[] identifyingAttributes,
        String[] identifyingValues) {
    Element parent = (Element) expectedChild.getParentNode();
    List<Element> allSiblingsWithMyName = getDirectChildren(parent, expectedChild.getTagName());
    int location = -1;
    for (Element sibling : allSiblingsWithMyName) {
        if (hasTheseAttributeValues(sibling, identifyingAttributes, identifyingValues)) {
            location++;//w  w  w  .ja va 2s. c  o m
        }
        if (sibling == expectedChild) {
            return location;
        }
    }
    return location;
}