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

/**
 * Retrieves the value of a child node given the name of the child node.
 * //w ww.  jav  a  2  s  . c om
 * @param node
 * @param childNodeName
 * @return
 */
public static String getChildValue(Node node, String childNodeName) {
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node n = children.item(i);
        if (n.getNodeName().equals(childNodeName)) {
            return getNodeValue(n);
        }
    }

    return null;
}

From source file:Main.java

public static Map<String, String> getChildElements(Node parent) {
    Map<String, String> elements = new HashMap<String, String>();

    NodeList children = parent.getChildNodes();

    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        elements.put(child.getNodeName(), child.getTextContent());
    }/*from www . j  av  a2s. c  o m*/

    return elements;
}

From source file:Main.java

public static void getNamespaces(Node node, Map<String, String> list) {
    NamedNodeMap atts = node.getAttributes();
    for (int i = 0; i < atts.getLength(); i++) {
        Node n = atts.item(i);
        if ("xmlns".equals(n.getNodeName())) {
            list.put(n.getNodeName(), n.getNodeValue());
        } else {/*from ww w  .  jav a2 s. c  om*/
            if (n.getNodeName().startsWith("xmlns:")) {
                list.put(n.getNodeName().substring(6), n.getNodeValue());
            }
        }
    }
}

From source file:Main.java

public static Properties extractProperties(Node node) {
    Properties props = new Properties();
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            Node item = attributes.item(i);
            props.put(item.getNodeName(), item.getNodeValue());
        }/*from w w  w . j  a  v  a 2s  . c  o m*/
    }
    return props;
}

From source file:Main.java

/**
 * Returns the main node of the XML document
 * /*from   ww  w .j av a2s .  c  om*/
 * @param doc the XML document
 * @return the main node
 */
public static Node getMainNode(Document doc) {
    for (int i = 0; i < doc.getChildNodes().getLength(); i++) {
        Node node = doc.getChildNodes().item(i);
        if (!node.getNodeName().equals("#text") && !node.getNodeName().equals("#comment")) {
            return node;
        }
    }
    throw new RuntimeException("main node in XML file could not be retrieved");
}

From source file:Main.java

static public String getNodeValue(String tagName, NodeList nodes) {
    for (int x = 0; x < nodes.getLength(); x++) {
        Node node = nodes.item(x);
        if (node.getNodeName().equalsIgnoreCase(tagName)) {
            NodeList childNodes = node.getChildNodes();
            for (int y = 0; y < childNodes.getLength(); y++) {
                Node data = childNodes.item(y);
                if (data.getNodeType() == Node.TEXT_NODE) {
                    return data.getNodeValue();
                }/*w  w  w .j  a va 2 s.  c o m*/
            }
        }
    }
    return "";
}

From source file:Main.java

private static boolean hasEqualAttributes(Node arg0, Node arg) {

    NamedNodeMap map1 = arg0.getAttributes();
    NamedNodeMap map2 = arg.getAttributes();
    int len = map1.getLength();
    if (len != map2.getLength()) {
        return false;
    }// w w  w .  ja va  2s  .co  m

    for (int i = 0; i < len; i++) {
        Node n1 = map1.item(i);
        if (n1.getNodeName() != null) {
            Node n2 = map2.getNamedItem(n1.getNodeName());
            if (n2 == null) {
                return false;
            } else if (!n1.getNodeValue().equals(n2.getNodeValue())) {
                return false;
            }
        }
    }
    return true;
}

From source file:Main.java

public static Node[] getChildNodesByName(Element parent, String name) {
    List<Node> nodeList = new ArrayList<Node>();

    NodeList childNodes = parent.getChildNodes();
    int length = childNodes.getLength();
    for (int i = 0; i < length; i++) {
        Node current = childNodes.item(i);
        if (current.getNodeName().equals(name))
            nodeList.add(current);//from ww w .  ja  v  a2s. co m
    }

    Node[] nodes = new Node[nodeList.size()];
    nodeList.toArray(nodes);
    return nodes;
}

From source file:Main.java

public static void getElementsByTagName(Node parent, String tagName, List<Node> result) {
    if (parent == null) {
        return;/*from  w ww. j  a va  2s.co  m*/
    }

    NodeList childs = parent.getChildNodes();
    for (int i = 0; i < childs.getLength(); i++) {
        Node child = childs.item(i);
        if (child.getNodeName().equals(tagName)) {
            result.add(child);
        } else if (child.hasChildNodes()) {
            getElementsByTagName(child, tagName, result);
        }
    }
}

From source file:Main.java

public static Node findChildWithTagName(Node parent, String tagName) {
    if (parent == null) {
        return null;
    }/*from www . jav  a2s.com*/

    NodeList childs = parent.getChildNodes();
    for (int i = 0; i < childs.getLength(); i++) {
        Node child = childs.item(i);
        if (child.getNodeName().equals(tagName)) {
            return child;
        } else if (child.hasChildNodes()) {
            Node result = findChildWithTagName(child, tagName);
            if (result != null) {
                return result;
            }
        }
    }

    return null;
}