Example usage for org.w3c.dom Node getNodeName

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

Introduction

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

Prototype

public String getNodeName();

Source Link

Document

The name of this node, depending on its type; see the table above.

Usage

From source file:Main.java

public static String path(Node n) {
    if (n == null)
        return "";
    switch (n.getNodeType()) {
    case Node.ATTRIBUTE_NODE: {
        return path(n.getParentNode()) + "/@" + n.getNodeName();
    }/*from   ww  w  .j  av  a  2s  .c  om*/
    case Node.TEXT_NODE: {
        return path(n.getParentNode()) + "/text()";
    }
    case Node.CDATA_SECTION_NODE: {
        return path(n.getParentNode()) + "/cdata()";
    }
    case Node.ELEMENT_NODE: {
        return path(n.getParentNode()) + "/" + n.getNodeName();
    }
    case Node.DOCUMENT_NODE: {
        return "";
    }
    default: {
        return "";
    }
    }
}

From source file:Main.java

/**
 * Get first child element with the provided node name and attribute that
 * are direct child the provided element.
 * /* ww w  . j a  v a2s  . co m*/
 * @param parent
 * @param name
 * @param attributeName
 * @param attributeValue
 * @return element if found, otherwise null
 */
public static Element getChildElementByNameAndAttribute(Element parent, String name, String attributeName,
        String attributeValue) {
    assertNotNull(parent);

    NodeList children = parent.getChildNodes();
    Node node;
    for (int i = 0; i < children.getLength(); i++) {
        node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name)) {
            Element element = (Element) node;
            if (element.getAttribute(attributeName).equals(attributeValue)) {
                return element;
            }
        }
    }

    return null;
}

From source file:Main.java

/**
 * @param n1 first Node to test//from   w  ww.  java2  s.  c  o m
 * @param n2 second Node to test
 * @return true if a deep compare show the same children and attributes in
 * the same order
 */
public static boolean equals(Node n1, Node n2) {
    // compare type
    if (!n1.getNodeName().equals(n2.getNodeName())) {
        return false;
    }
    // compare attributes
    NamedNodeMap nnm1 = n1.getAttributes();
    NamedNodeMap nnm2 = n2.getAttributes();
    if (nnm1.getLength() != nnm2.getLength()) {
        return false;
    }
    for (int i = 0; i < nnm1.getLength(); i++) {
        Node attr1 = nnm1.item(i);
        if (!getAttribute(n1, attr1.getNodeName()).equals(getAttribute(n2, attr1.getNodeName()))) {
            return false;
        }
    }
    // compare children
    Node c1 = n1.getFirstChild();
    Node c2 = n2.getFirstChild();
    for (;;) {
        while ((c1 != null) && c1.getNodeName().startsWith("#")) {
            c1 = c1.getNextSibling();
        }
        while ((c2 != null) && c2.getNodeName().startsWith("#")) {
            c2 = c2.getNextSibling();
        }
        if ((c1 == null) && (c2 == null)) {
            break;
        }
        if ((c1 == null) || (c2 == null)) {
            return false;
        }
        if (!equals(c1, c2)) {
            return false;
        }
        c1 = c1.getNextSibling();
        c2 = c2.getNextSibling();
    }
    return true;
}

From source file:Main.java

private static void findRecursive(Node parent, String name, List<Node> nodes, boolean onlyOne) {
    String nn = parent.getNodeName();
    int off = nn.indexOf(':');
    if (off >= 0) {
        nn = nn.substring(off + 1);//w ww  .  j av a 2 s .c  o  m
    }
    if (nn.equals(name)) {
        nodes.add(parent);
        if (onlyOne) {
            return;
        }
    }
    for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
        findRecursive(child, name, nodes, onlyOne);
        if (onlyOne && (nodes.size() > 0)) {
            return;
        }
    }
}

From source file:Main.java

public static Element getNextSiblingElement(Node node, final String siblingName) {
    do {/* w  w w . j av  a2  s  . com*/
        node = node.getNextSibling();
        if (node == null)
            return null;
    } while (node.getNodeType() != Node.ELEMENT_NODE || !node.getNodeName().equals(siblingName));
    return (Element) node;
}

From source file:Main.java

/**
 * Get the non qualified tag name/* w  ww .ja v  a 2s .  c om*/
 *
 * @param element element
 *
 * @return tag name
 */
public static String getLocalName(Node element) {
    String localName = element.getLocalName();
    if (localName != null) {
        return localName;
    }
    String name = element.getNodeName();
    int idx = name.indexOf(":");
    if (idx >= 0) {
        name = name.substring(idx + 1);
    }
    return name;
}

From source file:Main.java

protected static String nodeToString(Node node, Set<String> parentPrefixes, String namespaceURI)
        throws Exception {
    StringBuilder b = new StringBuilder();

    if (node == null) {
        return "";
    }/*from ww w  . j a  va2  s  . c o  m*/

    if (node instanceof Element) {
        Element element = (Element) node;
        b.append("<");
        b.append(element.getNodeName());

        Map<String, String> thisLevelPrefixes = new HashMap();
        if (element.getPrefix() != null && !parentPrefixes.contains(element.getPrefix())) {
            thisLevelPrefixes.put(element.getPrefix(), element.getNamespaceURI());
        }

        if (element.hasAttributes()) {
            NamedNodeMap map = element.getAttributes();
            for (int i = 0; i < map.getLength(); i++) {
                Node attr = map.item(i);
                if (attr.getNodeName().startsWith("xmlns"))
                    continue;
                if (attr.getPrefix() != null && !parentPrefixes.contains(attr.getPrefix())) {
                    thisLevelPrefixes.put(attr.getPrefix(), element.getNamespaceURI());
                }
                b.append(" ");
                b.append(attr.getNodeName());
                b.append("=\"");
                b.append(attr.getNodeValue());
                b.append("\"");
            }
        }

        if (namespaceURI != null && !thisLevelPrefixes.containsValue(namespaceURI)
                && !namespaceURI.equals(element.getParentNode().getNamespaceURI())) {
            b.append(" xmlns=\"").append(namespaceURI).append("\"");
        }

        for (Map.Entry<String, String> entry : thisLevelPrefixes.entrySet()) {
            b.append(" xmlns:").append(entry.getKey()).append("=\"").append(entry.getValue()).append("\"");
            parentPrefixes.add(entry.getKey());
        }

        NodeList children = element.getChildNodes();
        boolean hasOnlyAttributes = true;
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() != Node.ATTRIBUTE_NODE) {
                hasOnlyAttributes = false;
                break;
            }
        }
        if (!hasOnlyAttributes) {
            b.append(">");
            for (int i = 0; i < children.getLength(); i++) {
                b.append(nodeToString(children.item(i), parentPrefixes, children.item(i).getNamespaceURI()));
            }
            b.append("</");
            b.append(element.getNodeName());
            b.append(">");
        } else {
            b.append("/>");
        }

        for (String thisLevelPrefix : thisLevelPrefixes.keySet()) {
            parentPrefixes.remove(thisLevelPrefix);
        }

    } else if (node.getNodeValue() != null) {
        b.append(encodeText(node.getNodeValue()));
    }

    return b.toString();
}

From source file:Main.java

public static Node duplicate(Node node, Document ownerDocument) {
    if (node instanceof Text)
        return ownerDocument.createTextNode(node.getNodeValue());
    Node newNode = ownerDocument.createElement(node.getNodeName());
    NamedNodeMap attribs = node.getAttributes();
    for (int i = 0; i < attribs.getLength(); i++) {
        Node attrib = attribs.item(i);
        addAttribute(newNode, attrib.getNodeName(), attrib.getNodeValue());
    }/*  w  w  w .  j  av a2s .  c o  m*/
    for (Node n = node.getFirstChild(); n != null; n = n.getNextSibling()) {
        Node newN = duplicate(n, ownerDocument);
        newNode.appendChild(newN);
    }
    return newNode;
}

From source file:Main.java

public static Node findChildNode(Node node, String name) {
    Node child;
    NodeList list;/*from  w  w w. j  a  va 2s. c o  m*/

    list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        child = list.item(i);
        if (child instanceof Element && child.getNodeName().equals(name)) {
            return child;
        }
    }

    return null;
}

From source file:Main.java

/** Finds and returns the first child node with the given name. */
public static Element getFirstChildElement(Node parent, String elemName) {

    if (parent == null)
        return null;
    // search for node
    Node child = parent.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            if (child.getNodeName().equals(elemName)) {
                return (Element) child;
            }//www. j ava  2  s . com
        }
        child = child.getNextSibling();
    }

    // not found
    return null;

}