Example usage for org.w3c.dom Node getParentNode

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

Introduction

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

Prototype

public Node getParentNode();

Source Link

Document

The parent of this node.

Usage

From source file:DOMUtils.java

/**
 * Given a prefix and a node, return the namespace URI that the prefix
 * has been associated with. This method is useful in resolving the
 * namespace URI of attribute values which are being interpreted as
 * QNames. If prefix is null, this method will return the default
 * namespace./*from   www . ja v a  2 s . c o  m*/
 *
 * @param context the starting node (looks up recursively from here)
 * @param prefix the prefix to find an xmlns:prefix=uri for
 *
 * @return the namespace URI or null if not found
 */
public static String getNamespaceURIFromPrefix(Node context, String prefix) {
    short nodeType = context.getNodeType();
    Node tempNode = null;

    switch (nodeType) {
    case Node.ATTRIBUTE_NODE: {
        tempNode = ((Attr) context).getOwnerElement();
        break;
    }
    case Node.ELEMENT_NODE: {
        tempNode = context;
        break;
    }
    default: {
        tempNode = context.getParentNode();
        break;
    }
    }

    while (tempNode != null && tempNode.getNodeType() == Node.ELEMENT_NODE) {
        Element tempEl = (Element) tempNode;
        String namespaceURI = (prefix == null) ? getAttribute(tempEl, "xmlns")
                : getAttributeNS(tempEl, NS_URI_XMLNS, prefix);

        if (namespaceURI != null) {
            return namespaceURI;
        } else {
            tempNode = tempEl.getParentNode();
        }
    }

    return null;
}

From source file:Main.java

private static String getXPath(Node node, String xpath) {
    String elementName = "";
    if (node instanceof Element) {
        elementName = node.getNodeName();
        int prev_siblings = 1;
        Node prev_sibling = node.getPreviousSibling();
        while (null != prev_sibling) {
            if (prev_sibling.getNodeType() == node.getNodeType()) {
                if (prev_sibling.getNodeName().equalsIgnoreCase(node.getNodeName())) {
                    prev_siblings++;/*from w ww  .  j av a2s  .co m*/
                }
            }
            prev_sibling = prev_sibling.getPreviousSibling();
        }
        elementName = elementName.concat("[" + prev_siblings + "]");
    }
    Node parent = node.getParentNode();
    if (parent == null) {
        return xpath;
    }
    return getXPath(parent, "/" + elementName + xpath);
}

From source file:sce.RESTAppMetricJob.java

public static String parseKBResponse(Node node, JobExecutionContext context) throws JobExecutionException {
    String result = "";
    try {/*from w  w w . ja  v  a 2s .  c o m*/
        String parentNodeName = node.getParentNode().getNodeName();
        Node parentNodeAttribute = node.getParentNode().getAttributes() != null
                ? node.getParentNode().getAttributes().getNamedItem("name")
                : null;
        String parentParentNodeName = node.getParentNode().getParentNode() != null
                ? node.getParentNode().getParentNode().getNodeName()
                : "";
        if (/*node.getNodeName().equals("literal") &&*/parentNodeName.equals("binding")
                && parentNodeAttribute != null && parentParentNodeName.equals("result")) {
            result += "\n" + node.getNodeName() + ": " + node.getTextContent() + " "
                    + node.getParentNode().getAttributes().getNamedItem("name").getTextContent();
            context.setResult(context.getResult() != null ? context.getResult() + result : result);
            //createAndExecuteJob(node.getTextContent());
        }

        NodeList nodeList = node.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node currentNode = nodeList.item(i);
            if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                //calls this method for all the children which is Element
                parseKBResponse(currentNode, context);
            }
        }
        return result;
    } catch (DOMException | JobExecutionException e) {
        e.printStackTrace();
        throw new JobExecutionException(e);
    }
}

From source file:com.igormaznitsa.upom.UPomModel.java

private static void duplicatedSiblingJanitor(final Log log, final Node node, final List<String> path) {
    if (node == null) {
        return;//  www .  j av a2s .c om
    }

    insideElementJanitor(log, node, path);
    Node sibling = nextSiblingElement(node);
    while (sibling != null) {
        insideElementJanitor(log, sibling, path);
        sibling = nextSiblingElement(sibling);
    }

    sibling = nextSiblingElement(node);
    while (sibling != null) {
        if (node.isEqualNode(sibling)) {
            path.add(node.getNodeName());
            final Node deleting = sibling;
            sibling = nextSiblingElement(sibling);
            if (log != null) {
                log.warn("Removing duplicated element : " + pathToString(path));
            }
            deleting.getParentNode().removeChild(deleting);
            path.remove(path.size() - 1);
        } else {
            sibling = nextSiblingElement(sibling);
        }
    }
}

From source file:com.seer.datacruncher.utils.generic.CommonUtils.java

/**
 * That is a helping method that take Node and it iterate backword till root node and return the xpath expression
 * to reach that Node.//w  ww.  j ava  2  s  .co m
 * @param node
 * @return
 */
private static String fetchXPathXpressionOfNode(Node node) {
    String nodeXPathExpression = null;
    Node n = node.getParentNode();
    while (n != null) {
        if (n.getNodeName() != null
                && (n.getNodeName().endsWith("element") || n.getNodeName().endsWith("attribute"))) {
            if (nodeXPathExpression == null) {
                nodeXPathExpression = n.getAttributes().getNamedItem("name").getNodeValue();
            } else {
                nodeXPathExpression = n.getAttributes().getNamedItem("name").getNodeValue() + "/"
                        + nodeXPathExpression;
            }
        }
        n = n.getParentNode();
    }
    return nodeXPathExpression;
}

From source file:Main.java

/**
 * Generates XPath expression with the option of the Node values appended
 *
 * @param node             the Node whose XPath is to be found
 * @param ignoreWhitespace the flag to indicate if Whitespace will be ignored
 * @param includeValues    the flag to indicate if Node values will be included
 * @param noIndex          the flag to indicate if Node indexes are included
 * @return the XPath string representation of the Node
 *///from   w  w  w  .  ja v  a 2  s.c  om

public static String generateXPath(Node node, boolean ignoreWhitespace, boolean includeValues, boolean noIndex)

{

    boolean noValues = !includeValues;

    if (node == null)

        return "";

    Node parent = node.getParentNode();

    int index = noIndex ? 0 : getXPathNodeIndex(node, ignoreWhitespace);

    String indexStr = "";

    if (index > 0)

        indexStr = "[" + Integer.toString(index) + "]";

    //printNode(node);

    //printNode(parent);

    if (node.getNodeType() == Node.DOCUMENT_NODE)

    {

        // return only the blank String, since all the other types are preceded with /

        return "";

    } else if (node.getNodeType() == Node.TEXT_NODE)

    {

        return generateXPath(parent, ignoreWhitespace, noValues, noIndex) +

                (noValues ? node.getNodeValue() + indexStr : "/TEXT(" + node.getNodeValue() + ")" + indexStr);

    } else if (node.getNodeType() == Node.ELEMENT_NODE)

    {

        return generateXPath(parent, ignoreWhitespace, noValues, noIndex) +

                "/" + node.getNodeName() + indexStr;

    } else if (node.getNodeType() == Node.COMMENT_NODE)

    {

        return generateXPath(parent, ignoreWhitespace, noValues, noIndex) +

                (noValues ? node.getNodeValue() + indexStr
                        : "/COMMENT(" + node.getNodeValue() + ")" + indexStr);

    } else if (node.getNodeType() == Node.ENTITY_REFERENCE_NODE)

    {

        return generateXPath(parent, ignoreWhitespace, noValues, noIndex) +

                (noValues ? node.getNodeValue() + indexStr
                        : "/EntityReference(" + node.getNodeValue() + ")" + indexStr);

    } else if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE)

    {

        return generateXPath(parent, ignoreWhitespace, noValues, noIndex) +

                (noValues ? node.getNodeValue() + indexStr : "/PI(" + node.getNodeValue() + ")" + indexStr);

    } else if (node.getNodeType() == Node.ATTRIBUTE_NODE)

    {

        return generateXPath(((Attr) node).getOwnerElement(), ignoreWhitespace, noValues, noIndex) +

                "/'@" + node.getNodeName() +

                (noValues ? "" : "=" + node.getNodeValue()) + "]";

    } else if (node.getNodeType() == Node.DOCUMENT_TYPE_NODE)

    {

        return generateXPath(parent, ignoreWhitespace, noValues, noIndex) +

                (noValues ? node.getNodeValue() : "/DOCTYPE(" + node.getNodeName() + ")");

    } else if (node.getNodeType() == Node.CDATA_SECTION_NODE)

    {

        return generateXPath(parent, ignoreWhitespace, noValues, noIndex) +

                (noValues ? node.getNodeValue() : "/CDATA(" + node.getNodeName() + ")");

    }

    // Wont reach this far but just in case

    return "";

}

From source file:Main.java

/**
 * Resolves the xpath of an element.//ww  w .j  a  va  2  s .co m
 * 
 * @param elt
 * @return
 * @throws IOException
 */
public static final String getElementXPath(Node elt) throws IOException {
    String path = "";

    Node currentNode = elt;
    while (!(currentNode instanceof Document)) {
        Element parent = (Element) currentNode;
        if (!parent.getTagName().equals("schema")) {
            if (!parentNodeHasMoreOfThese((Element) currentNode)) {
                path = '/' + parent.getTagName() + path;
            } else {
                path = '/' + parent.getTagName() + '[' + getElementIdx(parent) + ']' + path;
            }
        } else {
            String schema = parent.getAttribute("name");
            String[] segments = path.substring(1).split(":", 2);
            return schema + ':' + segments[segments.length - 1];
        }

        currentNode = currentNode.getParentNode();
    }

    throw new IOException("Failed to parse document.");
}

From source file:bridge.toolkit.commands.S1000DConverter.java

/**
 * Remove the node from the DOM/*from   www . j av a2s  .  c  o  m*/
 * 
 * @param node
 * @throws Exception
 */
public static void removeNode(Node node) throws Exception {

    if (node != null)
        node.getParentNode().removeChild(node);
}

From source file:Main.java

/**
 * Generates XPath expression with the option of the Node values appended
 *
 * @param node             the Node whose XPath is to be found
 * @param parentXPath      the XPath of the parent Node
 * @param ignoreWhitespace the flag to indicate if Whitespace will be ignored
 * @param includeValues    the flag to indicate if Node values will be included
 * @param noIndex          the flag to indicate if Node indexes are included
 * @return the XPath string representation of the Node
 *///from   w  ww  .  j  a v  a2s .c  om

public static String generateXPath(Node node, String parentXPath, boolean ignoreWhitespace,
        boolean includeValues, boolean noIndex)

{

    boolean noValues = !includeValues;

    if (node == null)

        return "";

    Node parent = node.getParentNode();

    int index = noIndex ? 0 : getXPathNodeIndex(node, ignoreWhitespace);

    String indexStr = "";

    if (index > 0)

        indexStr = "[" + Integer.toString(index) + "]";

    if (node.getNodeType() == Node.DOCUMENT_NODE)

    {

        // return only the blank String, since all the other types are preceded with /

        return parentXPath + "";

    } else if (node.getNodeType() == Node.TEXT_NODE)

    {

        return parentXPath +

                (noValues ? "/" + node.getNodeValue() + indexStr
                        : "/TEXT(" + node.getNodeValue() + ")" + indexStr);

    } else if (node.getNodeType() == Node.ELEMENT_NODE)

    {

        return parentXPath +

                "/" + node.getNodeName() + indexStr;

    } else if (node.getNodeType() == Node.COMMENT_NODE)

    {

        return parentXPath +

                (noValues ? "/" + node.getNodeValue() + indexStr
                        : "/COMMENT(" + node.getNodeValue() + ")" + indexStr);

    } else if (node.getNodeType() == Node.ENTITY_REFERENCE_NODE)

    {

        return parentXPath +

                (noValues ? "/" + node.getNodeValue() + indexStr
                        : "/EntityReference(" + node.getNodeValue() + ")" + indexStr);

    } else if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE)

    {

        return parentXPath +

                (noValues ? "/" + node.getNodeValue() + indexStr
                        : "/PI(" + node.getNodeValue() + ")" + indexStr);

    } else if (node.getNodeType() == Node.ATTRIBUTE_NODE)

    {

        return parentXPath + "/[@" + node.getNodeName() +

                (noValues ? "" : "=" + node.getNodeValue()) + "]";

    } else if (node.getNodeType() == Node.DOCUMENT_TYPE_NODE)

    {

        return parentXPath +

                (noValues ? "/" + node.getNodeValue() : "/DOCTYPE(" + node.getNodeName() + ")");

    } else if (node.getNodeType() == Node.CDATA_SECTION_NODE)

    {

        return parentXPath +

                (noValues ? "/" + node.getNodeValue() : "/CDATA(" + node.getNodeName() + ")");

    }

    // Wont reach this far but just in case

    return "";

}

From source file:com.centeractive.ws.builder.soap.SchemaUtils.java

private static void selectDefinitionParts(String wsdlUrl, Map<String, XmlObject> existing, SchemaLoader loader,
        XmlObject xmlObject, String path) throws Exception {
    XmlObject[] wsdlImports = xmlObject.selectPath(path);
    for (int i = 0; i < wsdlImports.length; i++) {
        String location = ((SimpleValue) wsdlImports[i]).getStringValue();
        if (location != null) {
            if (StringUtils.isNotBlank(location)) {
                if (!location.startsWith("file:") && location.indexOf("://") == -1)
                    location = joinRelativeUrl(wsdlUrl, location);

                getDefinitionParts(location, existing, loader);
            } else {
                Node domNode = ((Attr) wsdlImports[i].getDomNode()).getOwnerElement();
                domNode.getParentNode().removeChild(domNode);
            }/*from  w w  w  .  j  ava 2  s  .  c  o m*/
        }
    }
}