Example usage for org.w3c.dom Node getLocalName

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

Introduction

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

Prototype

public String getLocalName();

Source Link

Document

Returns the local part of the qualified name of this node.

Usage

From source file:Main.java

public static Node getEnclosingNodeWithName(Node node, String name) {
    while (node != null) {
        if (node.getLocalName() != null && node.getLocalName().equals(name)) {
            return node;
        }// w w w .j av  a  2s .  c o m
        node = node.getParentNode();
    }
    return null;
}

From source file:Main.java

public static String getLocalName(Node node) {
    String localName = node.getLocalName();
    if (localName == null) {
        localName = node.getNodeName();/* ww w  .j a  v  a2s  .  c o m*/

        // If the document is not namespace aware, we must split the tag name
        // with ':' character.
        int i = localName.indexOf(':');
        if (i != -1) {
            localName = localName.substring(i + 1);
        }
    }

    return localName;
}

From source file:Main.java

private static int findNodeIndex(Node node) {
    String nm = node.getLocalName();
    String ns = node.getNamespaceURI();
    short nt = node.getNodeType();

    Node parentNode = node.getParentNode();
    if (parentNode.getNodeType() != Node.ELEMENT_NODE)
        return 1;

    Node child = parentNode.getFirstChild();

    int ix = 0;/*from  w w w  .  j a  v a2s  . c om*/
    while (child != null) {
        if (child == node)
            return ix + 1;

        if (child.getNodeType() == nt && nm.equals(child.getLocalName())
                && ((ns == null && child.getNamespaceURI() == null)
                        || (ns != null && ns.equals(child.getNamespaceURI()))))
            ix++;

        child = child.getNextSibling();
    }

    throw new RuntimeException("Child node not found in parent!?");
}

From source file:Main.java

public static QName nodeToQName(Node node) {
    return new QName(node.getNamespaceURI(), node.getLocalName());
}

From source file:Main.java

public static Node getChildNodebyName(Node parentNode, String nameOfChildToFind) {
    NodeList childNodes = parentNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node currentChildNode = childNodes.item(i);
        String childName = currentChildNode.getLocalName();
        if (childName != null)
            if (childName.equals(nameOfChildToFind))
                return currentChildNode;
    }//  ww  w  . j a  v  a  2s.co  m
    return null;
}

From source file:Main.java

private static void cleanWhiteList(Node node, ArrayList<String> whiteList) {
    if (whiteList.contains(node.getLocalName())) {
        node.setNodeValue(null);/* ww  w  .j  av a 2 s.  co m*/
        node.setTextContent(null);
        // System.err.println("haha");
    }
    NodeList children = node.getChildNodes();
    if (children.getLength() != 0) {
        for (int i = 0; i < children.getLength(); i++)
            cleanWhiteList(children.item(i), whiteList);
    }

}

From source file:Main.java

public static List<Node> getChildNodes(Node node, String childTag) {
    ArrayList<Node> nodes = new ArrayList<Node>();
    if (node == null)
        return nodes;
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node childNode = nodeList.item(i);
        if (childNode != null && childNode.getLocalName() != null
                && childNode.getLocalName().equals(childTag)) {
            nodes.add(childNode);// w w  w. j  a  v a  2s. c  om
        }
    }
    return nodes;
}

From source file:Main.java

/**
 * Get the a specific child node from its parent node by matching the local
 * name. If not found, return null.//from  w  ww. jav  a2  s. c o m
 *
 * @param parentNode
 *          the parent node.
 * @param localNodeName
 *          the local name of the node.
 * @return the node with the matching local name; otherwise, null if not
 * found.
 */
public static Node getChildNodeByLocalName(final Node parentNode, final String localNodeName) {
    final NodeList children = parentNode.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        final Node child = children.item(i);
        if (child.getLocalName() != null) {
            if (child.getLocalName().equals(localNodeName)) {
                return child;
            }
        }
    }
    return null;
}

From source file:Main.java

public static Node findNode(Node root, String tag) {
    if (root != null && root.getLocalName() != null && root.getLocalName().equals(tag)) {
        return root;
    }/*  w w w.  ja  v a2s .  com*/
    NodeList nodeList = root.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node childNode = nodeList.item(i);
        Node firstNode = findNode(childNode, tag);
        if (firstNode != null) {
            return firstNode;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Get the non qualified tag name/*from ww  w  . j a va  2  s. co m*/
 *
 * @param node node
 *
 * @return node name
 */
public static String getNodeName(Node node) {
    String localName = node.getLocalName();
    if (localName != null) {
        return localName;
    }
    String name = node.getNodeName();
    int idx = name.indexOf(":");
    if (idx >= 0) {
        name = name.substring(idx + 1);
    }
    return name;
}