Example usage for org.w3c.dom NamedNodeMap item

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

Introduction

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

Prototype

public Node item(int index);

Source Link

Document

Returns the indexth item in the map.

Usage

From source file:Main.java

/**
 * This grabs the attributes from a dom node and overwrites those values with those
 * specified by the overwrite map./*www . j  a  v  a  2  s.  com*/
 *
 * @param node node for building
 * @param overwrite map of attributes to overwrite
 * @return map of attributes
 */
public static Map<String, String> mapifyAttrs(Node node, Map<String, String> overwrite) {
    Map<String, String> map = new HashMap<String, String>();
    NamedNodeMap nnMap = node.getAttributes();
    for (int i = 0; i < nnMap.getLength(); i++) {
        Node attr = nnMap.item(i);
        map.put(attr.getNodeName(), attr.getNodeValue());
    }
    if (overwrite != null) {
        for (Map.Entry<String, String> e : overwrite.entrySet()) {
            map.put(e.getKey(), e.getValue());
        }
    }
    return map;
}

From source file:Main.java

public static void dump(Node node) {
    System.out.println("Node: " + node.getNodeName());
    NamedNodeMap nnm = node.getAttributes();
    if (nnm != null) {
        for (int i = 0; i < nnm.getLength(); i++) {
            Node n = nnm.item(i);
            System.out.println("   " + n.getNodeName() + ":" + n.getNodeValue());
        }//from   ww  w .j a  v a2s .c  o m
    }
}

From source file:Main.java

License:asdf

public static void dom(Element element) {
    NamedNodeMap attrs = element.getAttributes();

    int numAttrs = attrs.getLength();
    for (int i = 0; i < numAttrs; i++) {
        Attr attr = (Attr) attrs.item(i);
        String attrName = attr.getNodeName();
        System.out.println(attrName);
        String attrValue = attr.getNodeValue();
        System.out.println(attrValue);
    }/* w  w  w.  ja va 2s  .c  om*/
}

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>.// ww w  . j a  va  2  s  .  co  m
 *
 * @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

public static Map<String, String> getMapOfAttributes(Node elem) {
    Map<String, String> attrs = new HashMap<String, String>();
    NamedNodeMap m = elem.getAttributes();
    if (m != null) {
        for (int i = 0; i < m.getLength(); ++i) {
            Attr a = (Attr) m.item(i);
            attrs.put(a.getName(), a.getValue());
        }//from  ww  w  . j  a  v  a 2 s.  c  o  m
    }
    return attrs;
}

From source file:Main.java

public static String getNodeAttribute(Node node, String name) {

    if (node.hasAttributes()) {

        NamedNodeMap attrs = node.getAttributes();

        for (int i = 0; i < attrs.getLength(); i++) {
            Attr attribute = (Attr) attrs.item(i);

            if (attribute.getName().equals(name)) {
                return attribute.getValue();
            }/*from w w  w .  ja v  a 2  s .c om*/

        }
    }

    return null;

}

From source file:Main.java

public static Node findByID(Document doc, String id, String tagName) {
    NodeList node = doc.getElementsByTagName(tagName);
    for (int i = 0; i < node.getLength(); i++) {
        NamedNodeMap attributtes = node.item(i).getAttributes();
        for (int j = 0; j < attributtes.getLength(); j++)
            if (id.equalsIgnoreCase(attributtes.item(j).getNodeValue()))
                return node.item(i);
    }//from w w  w .  jav  a2  s.  c  om

    return null;
}

From source file:Main.java

/**
 * A method to get attribute value from provided xml node and attribute name
 * @param Node parent, a node where the attribute residing
 * @param String attr, attribute name to get
 * @return String , a value from request attribute 
 *//*from  ww  w.ja  v a  2  s .  co  m*/
static public String getAttribute(Node parent, String Attr) {
    String ret = "";

    if (!parent.hasAttributes())
        return ("");
    NamedNodeMap nmap = parent.getAttributes();

    for (int i = 0; i < nmap.getLength(); ++i) {
        Node n = nmap.item(i);
        if (n.getNodeName().trim().equals(Attr)) {
            ret = n.getNodeValue().trim();
        }
        //System.out.println("Attribute: "+n.getNodeType()+" - "+n.getNodeName()+" "+n.getNodeValue());

    }
    return (ret);
}

From source file:Main.java

public static String getNodeAttr(String attrName, Node node) {
    NamedNodeMap attrs = node.getAttributes();
    for (int y = 0; y < attrs.getLength(); y++) {
        Node attr = attrs.item(y);
        if (attr.getNodeName().equalsIgnoreCase(attrName)) {
            return attr.getNodeValue();
        }/*  w w w  . j ava2  s  . c o m*/
    }
    return "";
}

From source file:Main.java

/**
 * Remove all attribute from the specified element
 */// ww  w . j  av a  2s .c  o m
public static void removeAllAttributes(Element element) {
    final NamedNodeMap nodeMap = element.getAttributes();

    for (int i = 0; i < nodeMap.getLength(); i++)
        element.removeAttribute(nodeMap.item(i).getNodeName());
}