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

public static String getAttributeValue(Node node, String attribute) {
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        Node nameAttr = attributes.getNamedItem(attribute);
        if (nameAttr != null) {
            return nameAttr.getNodeValue();
        }/*from  www  . ja  v a2  s.  c  om*/
    }
    return null;
}

From source file:Main.java

public static String getAttributeValue(Node node, String name, String defaultValue) {
    try {/*from w  ww. j  a  v a2 s .  c  om*/
        return node.getAttributes().getNamedItem(name).getNodeValue();
    } catch (Exception ex) {
        return defaultValue;
    }
}

From source file:Main.java

/**
 * Returns the attribute value with the given id for the Node
 * /* w  ww.j  a  v a 2  s . c o m*/
 * @param n
 * @param id
 * @return
 */
public static String getAttribute(Node n, String id) {
    String result = null;
    NamedNodeMap attrMap = n.getAttributes();
    if (null != attrMap) {
        Node attr = attrMap.getNamedItem(id);
        result = (attr == null) ? null : attr.getNodeValue();
    }
    if (null != result) {
        result = result.trim();
    }
    return result;
}

From source file:Main.java

/**
 * @return the attribute value or null//from ww  w .j  av  a 2 s  .  c o  m
 */
public static String getAttributeValue(Node node, String attributeName) {
    NamedNodeMap attributes = node.getAttributes();
    if (attributes == null)
        return (null);

    Node tempNode = attributes.getNamedItem(attributeName);
    if (tempNode == null)
        return (null);

    return (tempNode.getNodeValue());
}

From source file:Main.java

/**
 * Setup the ID attribute into <code>destElement</code> depending on the <code>isId</code> flag of an attribute of
 * <code>sourceNode</code>./*from   w  ww  .  ja  v  a  2  s .  c om*/
 *
 * @param sourceNode
 * @param destDocElement
 */
public static void propagateIDAttributeSetup(Node sourceNode, Element destElement) {
    NamedNodeMap nnm = sourceNode.getAttributes();
    for (int i = 0; i < nnm.getLength(); i++) {
        Attr attr = (Attr) nnm.item(i);
        if (attr.isId()) {
            destElement.setIdAttribute(attr.getName(), true);
            break;
        }
    }
}

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   ww w .  j a  va2s.  c o m*/
 * @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;
}

From source file:Main.java

public static String getAttribute(Node pNode, String attrName) {
    try {//  w  ww .ja va2  s .  c  o  m
        NamedNodeMap name = pNode.getAttributes();
        if (name.getLength() > 0) {
            Node node = name.getNamedItem(attrName);
            if (node != null) {
                String attributeValue = name.getNamedItem(attrName).getNodeValue();
                return attributeValue;
            } else {
                return null;
            }
        } else {
            return null;
        }
    } catch (Exception e) {
        return null;
    }

}

From source file:Main.java

public static int getAlternativeIdCount(Node xmlSubstanceNode, boolean includeEmpty) {
    int idCnt = 0;
    int maxID = xmlSubstanceNode.getAttributes().getLength();
    for (int i = 1; i < maxID; i++) {
        Attr oAlternative = (Attr) xmlSubstanceNode.getAttributes().getNamedItem("name" + i);
        if (oAlternative != null) {
            String value = oAlternative.getNodeValue();
            if (includeEmpty || (value != null && value.length() > 0))
                idCnt++;/*  www.java  2 s  .co  m*/
        }
    }
    return idCnt;
}

From source file:Main.java

public static String getAttrvalue(Node item, String name, boolean ignoreNs) {
    NamedNodeMap attributes = item.getAttributes();
    int numAttrs = attributes.getLength();
    for (int i = 0; i < numAttrs; i++) {
        Attr attr = (Attr) attributes.item(i);
        String attrName = attr.getNodeName();
        String NSName = attr.getNamespaceURI();
        if (ignoreNs) {
            if ((attrName.indexOf(":" + name) != -1) || (name.equals(attrName)))
                return attr.getNodeValue();
        } else {/*  ww  w .  j a v a2 s . c om*/
            if (name.equals(attrName)) {
                return attr.getNodeValue();
            }
        }
    }
    return null;
}

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());
        }/*  w w  w .  ja va2  s .  co  m*/
    }
    return props;
}