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 void setIdentityAttribute(Document document, String attributeValue) {
    boolean isFound = false;
    NodeList propertyList = document.getElementsByTagName("Property");
    for (int i = 0; i < propertyList.getLength(); i++) {
        Node node = propertyList.item(i);
        for (int j = 0; j < node.getAttributes().getLength(); j++) {
            Node attribute = node.getAttributes().item(j);
            if (attribute.getNodeName().equals("name") && attribute.getNodeValue().equals(attributeValue)) {
                Element element = (Element) node;
                element.setAttribute("isIdentity", "true");
                isFound = true;//from   w  ww.ja v a2s .  c om
                break;
            }
        }
        if (isFound)
            break;
    }
}

From source file:Main.java

public static String getNodeAttr(String attrName, Node node) {
    NamedNodeMap attrs = node.getAttributes();
    for (int y = 0; y < attrs.getLength(); y++) {
        Node attr = attrs.item(y);
        if (attr.getNodeName().equalsIgnoreCase(attrName)) {
            return attr.getNodeValue();
        }//ww  w.j  a va2 s . c o m
    }
    return "";
}

From source file:Main.java

private static List<Node> getNode(Element element, String nodeWeiZhi) {
    List<Node> notes = new ArrayList<Node>();
    String[] nodeNames = nodeWeiZhi.split(">");
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        String nodeName = node.getNodeName();
        if (nodeName.equals(nodeNames[0])) {
            if (nodeNames.length == 1) {
                notes.add(node);/* ww  w  .  java 2  s  . c  om*/
            }
            String nodeWeiZhiTemp = nodeWeiZhi.replaceFirst(nodeNames[0], "").replaceFirst(">", "");
            NodeList childrenTempList = node.getChildNodes();
            if (childrenTempList.getLength() > 0 && !nodeWeiZhiTemp.equals("")) {
                notes.addAll(getNode((Element) node, nodeWeiZhiTemp));
            }
        }
    }
    return notes;
}

From source file:Main.java

public static Node getFirstChildByTagName(Node parent, String tagName) {
    NodeList nodeList = parent.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equalsIgnoreCase(tagName))
            return node;
    }/*w w w .  j a v a2 s  . c o  m*/
    return null;
}

From source file:Main.java

/**
 * Get specified child nodes by tag name.
 * /*ww  w  .ja  v  a2s .c o  m*/
 * @param parent
 *          the parent node
 * @param tagName
 *          the tag name
 * @param searchDeeper
 *          search deeper?
 * @return the child node list
 * @throws Exception
 *           on error
 */
public static List<Node> getChildNode(Node parent, String tagName, boolean searchDeeper) throws Exception {
    List<Node> list = new ArrayList<Node>();
    if (null != parent) {
        NodeList childrenList = parent.getChildNodes();
        int childrenCnt = childrenList.getLength();
        for (int i = 0; i < childrenCnt; i++) {
            Node child = childrenList.item(i);
            if (child.getNodeName().equals(tagName)) {
                list.add(child);
            }
            if (searchDeeper) {
                list.addAll(getChildNode(child, tagName, searchDeeper));
            }
        }
    }
    return list;
}

From source file:Main.java

/** Return first child with given name within given element. */
public static Element getFirst(Element parent, String name) {
    NodeList nl = parent.getChildNodes();
    for (int i = 0, l = nl.getLength(); i < l; i++) {
        Node curr = nl.item(i);
        if (curr.getNodeType() == Element.ELEMENT_NODE && curr.getNodeName().equals(name))
            return (Element) curr;
    }/*  w w  w.j  a  va  2  s .  co m*/

    return null;
}

From source file:Main.java

/**
 * Dumps a debug listing of the child nodes of the given node to
 * System.out.//  w w  w  . j av  a 2  s.c  om
 * 
 * @param node the node to dump the children of
 */
public static void dumpChildren(Node node) {
    System.out.println("Children of " + node.getNodeName() + ", NS: " + node.getNamespaceURI() + ", Type: "
            + node.getClass());
    Node child = node.getFirstChild();
    while (child != null) {
        short nodeType = child.getNodeType();
        String nodeName = child.getNodeName();
        String nodeValue = child.getNodeValue();
        String nsURI = child.getNamespaceURI();
        System.out.println("  Type: " + nodeType + ", Name: " + nodeName + ", Value: " + nodeValue + ", NS: "
                + nsURI + ", Type: " + node.getClass());
        child = child.getNextSibling();
    }
}

From source file:Main.java

/**
 * Parse the childnodes of a node and look for &lt;property&gt; elements with attributes name and value.
 * Example://from   www .  ja  v  a2 s  .  c  om
 * &lt;node&gt;
 *    &lt;property name='n' value='v'/&gt;
 * &lt;node/&gt;
 * @param n the XML node
 * @return the parsed properties
 */
public static Map<String, String> parseProperties(Node n) {
    NodeList propertyNodes = n.getChildNodes();
    Map<String, String> properties = new HashMap<String, String>();

    for (int i = 0; i < propertyNodes.getLength(); i++) {
        Node node = propertyNodes.item(i);

        if (node.getNodeName().equals("property")) {
            try {
                String key = node.getAttributes().getNamedItem("name").getNodeValue();
                String value = node.getAttributes().getNamedItem("value").getNodeValue();

                properties.put(key, value);
            } catch (NullPointerException e) {
                continue;
            }
        }
    }

    return properties;
}

From source file:Main.java

public static Element find(Element element, String name) {
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if ((child instanceof Element) && child.getNodeName().equals(name)) {
            return (Element) child;
        }/*from w  w  w .j av a2  s .c  om*/
    }
    return null;
}

From source file:Main.java

public static Node findNodeByAttribute(Document document, String tagName, String attributeName,
        String attributeValue) {/*from   w  w  w . j  a va 2 s  . c o m*/
    Node foundNode = null;
    NodeList nodes = document.getElementsByTagName(tagName);
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        for (int j = 0; j < node.getAttributes().getLength(); j++) {
            Node attribute = node.getAttributes().item(j);
            if (attribute.getNodeName().equals(attributeName)
                    && attribute.getNodeValue().equals(attributeValue)) {
                foundNode = node;
                break;
            }
        }
        if (foundNode != null)
            break;
    }
    return foundNode;
}