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 Color toColor(Node n) {
    if (!n.getNodeName().equals("color")) {
        throw new IllegalArgumentException(n.getNodeName());
    }//from w  w  w. j a va 2s .com
    NamedNodeMap map = n.getAttributes();
    String s = map.getNamedItem("name").getNodeValue();
    if (s.equals("white")) {
        return Color.WHITE;
    } else if (s.equals("green")) {
        return Color.GREEN;
    } else if (s.equals("pink")) {
        return Color.PINK;
    } else if (s.equals("cyan")) {
        return Color.CYAN;
    } else if (s.equals("yellow")) {
        return Color.YELLOW;
    } else {
        return Color.WHITE;
    }
}

From source file:Main.java

/**
 * Gets the content of an attribute./*w  w  w  . java 2 s.  co m*/
 * For example,
 * <item attributeName="content" />
 */
public static Optional<String> getAttributeContent(Node item, String attributeName) {
    NamedNodeMap attributes = item.getAttributes();
    return Optional.ofNullable(attributes.getNamedItem(attributeName)).map(Node::getTextContent);
}

From source file:Main.java

public static Node getAttributeNode(Node sNode, String attribName) {
    NamedNodeMap attrs = sNode.getAttributes();
    if (attrs != null) {
        return attrs.getNamedItem(attribName);
    }//  w w w  .  j ava  2 s . co  m
    return null;
}

From source file:Main.java

public static void removeAllAttributes(Node node, String attrName) {

    // check if this node contains the attribute and remove it
    NamedNodeMap attrs = node.getAttributes();
    if (attrs != null && attrs.getNamedItem(attrName) != null) {
        attrs.removeNamedItem(attrName);
    }//from ww w.jav a  2  s .  c  om

    // process recursively all children
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        // Get child node
        Node childNode = list.item(i);

        // Visit child node
        removeAllAttributes(childNode, attrName);
    }
}

From source file:Main.java

public static boolean setAttributeValue(final Element target, final String attributeName, final String value) {
    final NamedNodeMap map = target.getAttributes();
    final Node attributeValueHolder = map.getNamedItem(attributeName);
    if (attributeValueHolder != null) {
        attributeValueHolder.setNodeValue(value);
        return true;
    }/* w  w  w .ja va2  s .  com*/
    return false;
}

From source file:Main.java

static String getNamedItemNodeValue(NamedNodeMap attributes, String name, String defvalue) {
    Node namenode = attributes.getNamedItem(name);
    if (namenode == null) {
        return defvalue;
    }// w  ww .  j av  a  2s.c om
    if (namenode.getNodeValue() == null) {
        return defvalue;
    }
    return namenode.getNodeValue();
}

From source file:Main.java

/**
 * Getter for the attribute value of an attribute of the given name
 * for the specified node. The attribute value is returned as a string.
 * @param node Node.//from www. j  a  v  a  2  s  .  c  o  m
 * @param name Name of the attribute.
 * @return Returns the attribute value as string or null if the attribute
 * does not exist.
 */
static public String getAttrValString(Node node, String name) {
    NamedNodeMap attr = node.getAttributes();
    if (attr != null) {
        node = attr.getNamedItem(name);
        if (node != null)
            return (node.getNodeValue());
    }
    return (null);
}

From source file:Main.java

/**
 *  Gets the node value as date.//from  ww  w.j  av  a  2s .c  o  m
 *
 *@param  node                          Description of the Parameter
 *@return                               The nodeValueAsDate value
 *@exception  DOMException              Description of the Exception
 *@exception  ParseException  Description of the Exception
 */
public final static Date getNodeValueAsDate(Node node) throws DOMException, ParseException {
    if (node == null)
        return null;

    NamedNodeMap attrs = node.getAttributes();
    Node attr = attrs.getNamedItem("DateTimeFormat");

    // Date format
    String format = attr.getNodeValue().trim();
    node = node.getFirstChild();
    if (node != null) {
        String date = node.getNodeValue().trim();
        DateFormat df = new SimpleDateFormat(format);
        return df.parse(date);
    }
    return null;
}

From source file:Main.java

public static String getAttributeValue(NamedNodeMap attribs, String attributeName) throws DOMException {
    String value = null;/* ww  w  .  j  ava  2s .c o m*/
    if (attribs.getNamedItem(attributeName) != null)
        value = attribs.getNamedItem(attributeName).getNodeValue();
    return value;
}

From source file:Main.java

public static String getNodeAttribute(Node n, String name) {
    if (n == null)
        return null;

    NamedNodeMap attr = n.getAttributes();
    Node nameNode = attr.getNamedItem(name);
    if (nameNode != null) {
        String name_value = getNodeValue(nameNode);
        return name_value;
    } else {//from   ww w .jav  a 2 s.c  o m
        return null;
    }
}