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 getNamespaceURI(final org.w3c.dom.Node n, final String prefix) {
    final Node prefixDeclaration = n.getAttributes().getNamedItem("xmlns:" + prefix);
    if (prefixDeclaration != null) {
        // we have found the good NameSpace
        return prefixDeclaration.getNodeValue();
    }/*from  w  w w. ja va 2 s.c om*/
    // we have found the good NameSpace
    // we look for the NameSpace in the parent Node
    return getNamespaceURI(n.getParentNode(), prefix);
}

From source file:Main.java

public static String getAttributeValue(Node node, String attrName) throws TransformerException {
    String value = null;//from  w ww . ja v a  2 s  .  c o  m
    node = node.getAttributes().getNamedItem(attrName);
    if (node != null) {
        value = node.getNodeValue();
    }
    return value;
}

From source file:Main.java

public static void removeAttribute(Node parent, String name, String value, boolean recursive) {
    NamedNodeMap nnm = parent.getAttributes();
    if (nnm != null) {
        if (value == null) {
            nnm.removeNamedItem(name);//from   w w w  .j  a v  a  2  s . c o m
        } else {
            Node attr = nnm.getNamedItem(name);
            if (attr != null) {
                String attrVal = attr.getNodeValue();
                if (value.equals(attrVal)) {
                    nnm.removeNamedItem(name);
                }
            }
        }
    }
    if (recursive) {
        for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
            removeAttribute(child, name, value, recursive);
        }
    }
}

From source file:Main.java

public static String getAttributeValue(Node node, String str) {
    if (node == null || str == null) {
        return null;
    }/*  w  w w . j  av  a2 s. c o  m*/
    Node namedItem = node.getAttributes().getNamedItem(str);
    return namedItem != null ? namedItem.getNodeValue() : null;
}

From source file:Main.java

public static String getAttrsAsString(Node node) {

    if (node == null)
        return "";

    NamedNodeMap attrs = node.getAttributes();
    StringBuilder val = new StringBuilder();

    if (attrs != null) {
        for (int i = 0; i < attrs.getLength(); i++) {
            Node nval = attrs.item(i);

            if (nval != null) {
                if (i > 0)
                    val.append(", ");

                val.append(nval.getNodeName()).append("=").append(nval.getNodeValue());
            }//w  w  w . j  a  v  a2 s  .co m
        }
    }

    return val.toString().trim();
}

From source file:Main.java

/**
 * @param node//from   ww w .java  2  s .c o  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;
}

From source file:Main.java

/**
 * Get the attribute with given name's value
 * //from  ww w  . ja  v  a  2s  .com
 * @param node
 *            the node which attribute's value is returned
 * @param name
 *            name of the attribute
 * @return the value af the attribute
 */
public static String getAttributeByName(Node node, String name) {
    if (node == null) {
        return null;
    }

    Node attribute = node.getAttributes().getNamedItem(name);
    if (attribute == null) {
        return null;
    } else {
        return attribute.getNodeValue().trim();
    }
}

From source file:Main.java

/**
 * Extract included filenames in the XML document, assuming that filenames are
 * provided with the attribute "href".//from w  ww .  ja  v a 2 s.c  o  m
 * 
 * @param xmlDocument the XML document
 * @return the filenames to include
 */
private static Vector<String> extractIncludedFiles(Document xmlDocument) {

    Vector<String> includedFiles = new Vector<String>();

    NodeList top = xmlDocument.getChildNodes();
    for (int i = 0; i < top.getLength(); i++) {
        Node topNode = top.item(i);
        NodeList firstElements = topNode.getChildNodes();
        for (int j = 0; j < firstElements.getLength(); j++) {
            Node midNode = firstElements.item(j);
            for (int k = 0; k < midNode.getChildNodes().getLength(); k++) {
                Node node = midNode.getChildNodes().item(k);
                if (node.hasAttributes() && node.getAttributes().getNamedItem("href") != null) {
                    String fileName = node.getAttributes().getNamedItem("href").getNodeValue();
                    includedFiles.add(fileName);
                }
            }

        }
    }
    return includedFiles;
}

From source file:Main.java

/**
 * Search a child node by type// w  ww .  j a  v  a 2 s  .  c  o m
 * 
 * @param parent   the parent node
 * @param nodeType  the node type for searching
 * @return         Node with the specified name
 * @see            Node
 * @throws Exception
 */
public static Node findChildNodeByType(Node parent, String nodeType) throws Exception {
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node.getNodeName().equals("Node")) {
            String strType = node.getAttributes().getNamedItem("type").getNodeValue();
            if (strType.equals(nodeType))
                return node;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Finds attribute of node by name.//from   w  w w . j a  va 2 s.  c om
 * @param node Node The context node.
 * @param name String
 * @return Node
 */
public static Node findAttribute(Node node, String name) {
    Node found = null;
    NamedNodeMap nm = node.getAttributes();
    if (nm != null) {
        for (int i = 0; i < nm.getLength(); i++) {
            Node attr = nm.item(i);
            if (attr.getNodeName().compareToIgnoreCase(name) == 0) {
                found = attr;
                break;
            }
        }
    }
    return found;
}