Example usage for org.w3c.dom NamedNodeMap getNamedItem

List of usage examples for org.w3c.dom NamedNodeMap getNamedItem

Introduction

In this page you can find the example usage for org.w3c.dom NamedNodeMap getNamedItem.

Prototype

public Node getNamedItem(String name);

Source Link

Document

Retrieves a node specified by name.

Usage

From source file:Main.java

public static boolean nodeMatchesAttributeFilter(Node node, String str, List<String> list) {
    if (str == null || list == null) {
        return true;
    }/*ww w.  ja v a2 s. com*/
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        Node namedItem = attributes.getNamedItem(str);
        if (namedItem != null && list.contains(namedItem.getNodeValue())) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

/**
 * get the value of an Attribute in the Xml Document.
 * finds the first occurance of the parent element and then searches its attributes
 * for the occurance of the attribute and retrieves its value.
 * @param root the root Element.//  ww  w  .  j av  a2s  .com
 * @param elemName the name of the element to search for.
 * @param att the name of the attribute  to search for.
 * @return String the attribute value or null if not found.
 */
public static String getElementAttribute(Element root, String elemName, String att) {
    NodeList nl = root.getElementsByTagName(elemName);
    if (null == nl) {
        return (null);
    }
    Node n = nl.item(0);
    if (null == n) {
        return (null);
    }
    NamedNodeMap attributes = n.getAttributes();
    if (null == attributes) {
        return (null);
    }
    n = attributes.getNamedItem(att);
    if (null == n) {
        return (null);
    }
    return (n.getNodeValue().trim());

}

From source file:Main.java

public static ArrayList<Node> getNodesWithKey(Node parent, String key, Set<String> values,
        boolean all_of_them) {
    ArrayList<Node> valid = new ArrayList<Node>();
    NodeList children = parent.getChildNodes();
    Node current;/* w w  w.  j ava  2  s .  c o  m*/
    for (int i = 0; i < children.getLength(); i++) {
        current = children.item(i);
        NamedNodeMap attrs = current.getAttributes();
        if (attrs != null) {
            Node keynode = attrs.getNamedItem(key);
            if (keynode != null)
                if (values == null || values.contains(keynode.getNodeValue())) {
                    valid.add(current);
                    if (!all_of_them)
                        break;
                }
        }
    }
    return valid;
}

From source file:Main.java

/**
 * @param vertexStyleNode// w ww  . j  a  v a  2 s . co  m
 * @return the WKN
 */
public static String toWellKnowName(Node vertexStyleNode) {
    if (vertexStyleNode == null) {
        return "";
    }

    String value = "square";
    try {

        NamedNodeMap atts = vertexStyleNode.getAttributes();
        String nodeVal = atts.getNamedItem("class").getNodeValue();

        if (nodeVal.indexOf("Square") > -1) {
            // already there
        } else if (nodeVal.indexOf("Circle") > -1) {
            value = "circle";
        } else if (nodeVal.indexOf("Cross") > -1) {
            value = "cross";
        } else if (nodeVal.indexOf("Star") > -1) {
            value = "star";
        } else if (nodeVal.indexOf("Triangle") > -1) {
            value = "triangle";
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return value;
}

From source file:Main.java

/**
 * Gets value of an attribute from node. Returns default value if attribute
 * not found.//  w  w  w  .j a va 2 s .c  om
 * 
 * @param node
 *            Node Object
 * @param attributeName
 *            Attribute Name
 * @param defaultValue
 *            Default value if attribute not found
 * @return Attribute Value
 * @throws IllegalArgumentException
 *             for Invalid input
 */
public static String getAttribute(final Node node, final String attributeName, final String defaultValue)
        throws IllegalArgumentException {
    // Validate attribute name
    if (attributeName == null) {
        throw new IllegalArgumentException(
                "Attribute Name " + " cannot be null in XmlUtils.getAttribute method");
    }

    // Validate node
    if (node == null) {
        throw new IllegalArgumentException("Node cannot " + " be null in XmlUtils.getAttribute method for "
                + "Attribute Name:" + attributeName);
    }

    final NamedNodeMap attributeList = node.getAttributes();
    final Node attribute = attributeList.getNamedItem(attributeName);

    // Validate attribute name
    if (attribute == null) {
        return defaultValue;
    }

    return ((Attr) attribute).getValue();
}

From source file:Main.java

/**
 * @param n Node to examine//from w  ww  .  j  a  v  a  2 s .  c  o  m
 * @param attr Attribute to look for
 * @param def Default value to return if attribute is not present
 * @return if the Node contains the named Attribute, the value, if not, the def parameter
 */
public static String getAttribute(Node n, String attr, String def) {
    NamedNodeMap attrs = n.getAttributes();
    if (attrs == null)
        return def;
    Node ret = attrs.getNamedItem(attr);
    if (ret == null)
        return def;
    else
        return ret.getNodeValue();
}

From source file:Main.java

/**
 * Get the attribute value from specified node.
 * //from   w  w  w . j  a va 2s .  co  m
 * @param node
 *          the node
 * @param attributeName
 *          the attribute name
 * @return the attribut value
 */
public static String getNodeAttribute(Node node, String attributeName) {
    if (null == node) {
        return null;
    }
    NamedNodeMap attributes = node.getAttributes();
    if (null == attributes) {
        return null;
    }
    Node n = attributes.getNamedItem(attributeName);
    if (null == n) {
        return null;
    }
    return n.getNodeValue();
}

From source file:Main.java

/**
 * Returns {@code true} iff the node has the attribute {@code attributeName} with a value that
 * matches one of {@code attributeValues}.
 *//*from www . ja va 2 s . c o m*/
static boolean nodeMatchesAttributeFilter(final Node node, final String attributeName,
        final List<String> attributeValues) {
    if (attributeName == null || attributeValues == null) {
        return true;
    }

    final NamedNodeMap attrMap = node.getAttributes();
    if (attrMap != null) {
        Node attrNode = attrMap.getNamedItem(attributeName);
        if (attrNode != null && attributeValues.contains(attrNode.getNodeValue())) {
            return true;
        }
    }

    return false;
}

From source file:Main.java

/**
 * Gets a string value of the given attribute
 * @param node/*  w  ww. j  a va  2s.  c o m*/
 * @param attrName
 * @param defaultValue
 * @return
 */
public static String getValue(Node node, String attrName, String defaultValue) {
    Node tmp;
    if (node == null)
        return defaultValue;

    NamedNodeMap attrs = node.getAttributes();
    if (attrs == null)
        return defaultValue;

    String value = (tmp = attrs.getNamedItem(attrName)) != null ? tmp.getNodeValue() : null;

    if (value == null)
        return defaultValue;

    return value;
}

From source file:Main.java

/**
 * @param node/*from w ww.j  ava  2  s .co m*/
 * @param attributeName
 * @return
 */
public static String getNodeAttribute(Node node, String attributeName) {
    String sRetValue = null;
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        //--- Find the attribute 
        Node attrib = attributes.getNamedItem(attributeName);
        if (attrib != null) {
            //--- Get the attribute value
            sRetValue = attrib.getNodeValue();
        }
    }
    return sRetValue;
}