Example usage for org.w3c.dom Node getAttributes

List of usage examples for org.w3c.dom Node getAttributes

Introduction

In this page you can find the example usage for org.w3c.dom Node getAttributes.

Prototype

public NamedNodeMap getAttributes();

Source Link

Document

A NamedNodeMap containing the attributes of this node (if it is an Element) or null otherwise.

Usage

From source file:Main.java

/**
 *  Gets the node value as time./*from ww  w. j a  v  a  2 s  .  c om*/
 *
 *@param  node              Description of the Parameter
 *@return                   The nodeValueAsTime value
 *@exception  DOMException  Description of the Exception
 */
public final static long getNodeValueAsTime(Node node) throws DOMException {
    if (node == null)
        return -1;

    NamedNodeMap attrs = node.getAttributes();
    Node attr = attrs.getNamedItem("Unit");
    int factor = 1;
    String unit = attr.getNodeValue();
    if (unit.equals("sec")) {
        factor = 1000;
    } else if (unit.equals("min")) {
        factor = 60000;
    } else if (unit.equals("hr")) {
        factor = 3600000;
    } else if (unit.equals("day")) {
        factor = 86400000;
    }

    node = node.getFirstChild();
    if (node != null) {
        String time = node.getNodeValue().trim();
        return Integer.parseInt(time) * factor;
    }
    return -1;
}

From source file:Main.java

/**
 * Adds the attribute to the given node/*from   w w w .  ja v a 2  s.  c o m*/
 * @param doc
 * @param node
 * @param attrName
 * @param attrValue
 */
public static void addAttr(Document doc, Node node, String attrName, Object attrValue) {
    Node attr = doc.createAttribute(attrName);
    attr.setNodeValue(attrValue.toString());
    node.getAttributes().setNamedItem(attr);
}

From source file:Main.java

/**
 * Extracts from node the attribute with the specified name.
 * @param node the node whose attribute we'd like to extract.
 * @param name the name of the attribute to extract.
 * @return a String containing the trimmed value of the attribute or null
 * if no such attribute exists//w w  w . j av a 2 s  .  com
 */
public static String getAttribute(Node node, String name) {
    if (node == null)
        return null;

    Node attribute = node.getAttributes().getNamedItem(name);
    return (attribute == null) ? null : attribute.getNodeValue().trim();
}

From source file:Main.java

public static void setAttribute(Node node, String key, String value) {
    Node attributeNode = node.getOwnerDocument().createAttribute(key);
    attributeNode.setNodeValue(value);/*ww  w. ja  v  a 2 s  .  com*/

    node.getAttributes().setNamedItem(attributeNode);
}

From source file:Main.java

public static String getPrefixNS(String uri, Node e) {
    while (e != null && e.getNodeType() == Node.ELEMENT_NODE) {
        NamedNodeMap attrs = e.getAttributes();
        for (int n = 0; n < attrs.getLength(); n++) {
            Attr a = (Attr) attrs.item(n);
            String name = a.getName();
            if (name.startsWith("xmlns:") && a.getNodeValue().equals(uri)) {
                return name.substring("xmlns:".length());
            }// w w w .j  a  v  a2  s .c  om
        }
        e = e.getParentNode();
    }
    return null;
}

From source file:Main.java

public static Attr addAttribute(Node parent, String name, String value) {
    Attr node = parent.getOwnerDocument().createAttribute(name);
    node.setValue(value);//w  ww . jav  a  2 s  . c  o m
    parent.getAttributes().setNamedItem(node);
    return node;
}

From source file:Main.java

/**
   Return the value of an attribute of a node as a boolean variable
   or <code>null</code> if the attribute is not present.
 *///  ww  w . j ava2  s . c  o  m
static public Boolean getBooleanAttribute(Node node, String att_name) {
    if (node == null)
        return null;
    String text = getText(node.getAttributes().getNamedItem(att_name));
    if (text == null)
        return null;
    return Boolean.valueOf(text);
}

From source file:Main.java

/**
   Return the value of an attribute of a node as a real number
   or <code>null</code> if the attribute is not present.
 *//*from  w ww .j ava  2 s. c om*/
static public Double getDoubleAttribute(Node node, String att_name) {
    if (node == null)
        return null;
    String text = getText(node.getAttributes().getNamedItem(att_name));
    if (text == null)
        return null;
    return Double.valueOf(text);
}

From source file:Main.java

/**
   Return the value of an attribute of a node as an integer number
   or <code>null</code> if the attribute is not present.
 *//*  ww w  . jav  a2  s. c o  m*/
static public Integer getIntegerAttribute(Node node, String att_name) {
    if (node == null)
        return null;
    String text = getText(node.getAttributes().getNamedItem(att_name));
    if (text == null)
        return null;
    return Integer.valueOf(text);
}

From source file:Main.java

public static Point toPoint(Node n) {
    if (!n.getNodeName().equals("center")) {
        throw new IllegalArgumentException(n.getNodeName());
    }//ww w  .  j av a2 s.  com
    NamedNodeMap map = n.getAttributes();
    int x = Integer.parseInt(map.getNamedItem("x").getNodeValue());
    int y = Integer.parseInt(map.getNamedItem("y").getNodeValue());
    return new Point(x, y);
}