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

/**
 * @param node//from w w w .  ja  v a  2 s.  co m
 * @param name
 * @return all children nodes whose names contain <code>like</code>
 */
public static List<Node> findChildrenLike(Node node, String like) {
    List<Node> ret = new LinkedList<Node>();
    NodeList nl = node.getChildNodes();
    int len = nl.getLength();
    Node child;
    for (int i = 0; i < len; i++) {
        child = nl.item(i);
        if (child.getNodeName().indexOf(like) > 0) {
            ret.add(child);
        }
    }
    return ret;
}

From source file:Main.java

public static Element findNextElement(Node ret, String name) {
    ret = ret.getNextSibling();/*from ww w  .  j  av a2s .c om*/
    while (ret != null && (!(ret instanceof Element) || !ret.getNodeName().equals(name))) {
        ret = ret.getNextSibling();
    }
    return (Element) ret;
}

From source file:Main.java

public static String getPath(Node node) {
    String path = "";
    Node current = node;
    while ((current != null) && !(current instanceof Document)) {
        path = current.getNodeName() + "/" + path;
        current = current.getParentNode();
    }/*from w  w  w . ja  va  2s .  c o m*/
    if (path != null) {
        path = path.substring(0, path.lastIndexOf("/"));
    }
    return path;
}

From source file:Main.java

public static String nodeToString(Node node) {
    String ret = "<null/>";
    if (node != null) {
        ret = "<" + node.getNodeName() + " ";
        NamedNodeMap attrs = node.getAttributes();
        if (attrs != null) {
            for (int i = 0; i < attrs.getLength(); i++) {
                Node attr = (Node) attrs.item(i);
                ret = ret + attr.getNodeName() + "='";
                ret = ret + attr.getNodeValue() + "' ";
            }//  w  w w .  j  a  v  a  2  s . com
        }
        ret = ret + "/>";
    }
    return ret;
}

From source file:Main.java

/**
 * Returns all children of the given element which are element named as
 * specified./*  www  .  ja v  a  2s.c  o m*/
 */
public static List<Element> getNamedChildren(Element element, String elementNames) {
    List<Element> result = new ArrayList<Element>();
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        Node node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(elementNames)) {
            result.add((Element) node);
        }
    }
    return result;
}

From source file:Main.java

/**
 * Get all attributes as a Map from a node
 *
 * @param node the node taht contains the attributes to read
 * @return the values or a empty map/*from  w w  w  . j av  a2 s. c  om*/
 */
public static HashMap<String, String> getAttributes(Node node) {
    final HashMap<String, String> result = new HashMap<>();

    final NamedNodeMap atts = node.getAttributes();
    for (int i = 0; i < atts.getLength(); i++) {
        final Node att = atts.item(i);
        result.put(att.getNodeName(), att.getNodeValue());
    }
    return result;
}

From source file:Main.java

public static List<String> getValuesFromDocumentByTagAndAttribute(Node parentNode, String tagName,
        String attributeName, String attributeValue) {
    ArrayList<String> values = new ArrayList<String>();

    for (int i = 0; i < parentNode.getChildNodes().getLength(); i++) {
        Node childNode = parentNode.getChildNodes().item(i);

        if (childNode != null) {
            if (childNode.hasAttributes()) {
                for (int j = 0; j < childNode.getAttributes().getLength(); j++) {
                    Node attribute = childNode.getAttributes().item(j);
                    if (attribute.getNodeName().equals(attributeName)
                            && attribute.getNodeValue().equals(attributeValue)) {
                        values.add(childNode.getTextContent().trim());
                    }/*w ww .j  a  v  a2 s  . c  om*/
                }
            }

            if (childNode.hasChildNodes()) {
                values.addAll(getValuesFromDocumentByTagAndAttribute(childNode, tagName, attributeName,
                        attributeValue));
            }
        }
    }

    return values;
}

From source file:Main.java

public static Element GetChildElement(Element element, String name) {
    if (element != null && name != null && !name.isEmpty()) {
        NodeList nodes = element.getChildNodes();

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

            if (node instanceof Element && name.equals(node.getNodeName())) {
                return (Element) node;
            }/* w ww.  j  av a  2  s. c  om*/
        }
    }

    return null;
}

From source file:Main.java

/**
 * //from   ww w.  ja va  2s .  c om
 * @param node
 * @param classname
 * @return
 */
public static Object decode(Element node, String classname) {
    try {
        Class classObject = Class.forName(classname);
        Object object = classObject.newInstance();
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node child = attributes.item(i);
            String nodeName = child.getNodeName();
            Field field = classObject.getField(nodeName);
            field.setAccessible(true);
            field.set(object, child.getNodeValue());
        }
        return object;
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        return null;
    } catch (InstantiationException e) {
        e.printStackTrace();
        return null;
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        return null;
    } catch (SecurityException e) {
        e.printStackTrace();
        return null;
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:Main.java

/**
 * Compares the attributes of two XML <code>Node</code> objects. This method
 * returns <code>true</code> if all attribute name-value pairs match 
 * disregarding their order of placement.
 * /*from  w w w  .  ja  v  a  2s.  c  om*/
 * @param   n1    first <code>Node</code>
 * @param   n2    second <code>Node</code>
 * 
 * @return  boolean
 * 
 */
public static boolean diffAttributes(Node n1, Node n2) {
    NamedNodeMap n1Atts = n1.getAttributes();
    NamedNodeMap n2Atts = n2.getAttributes();

    if (n1Atts.getLength() != n2Atts.getLength()) {
        return false;
    }

    for (int i = 0; i < n1Atts.getLength(); i++) {
        Node a1 = n1Atts.item(i);
        Node a2Val = n2Atts.getNamedItem(a1.getNodeName());
        if (a2Val == null || !a1.getNodeValue().equals(a2Val.getNodeValue())) {
            return false;
        }
    }
    return true;
}