Example usage for org.w3c.dom NamedNodeMap getLength

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

Introduction

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

Prototype

public int getLength();

Source Link

Document

The number of nodes in this 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.//ww  w  . java  2  s  .  c  o m
 *
 * @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

/**
 * This method will compare the attributes of a given src element with the attributes of a given dest element.
 * Both must contain the same attributes, but you can specify a String array of attribute names whose values
 * are ignored. Both elements must have the ignore attributes, their contents can be different and they will
 * still be considered equal.//from  www.jav  a 2 s.c om
 * @param src - the src element whose attributes are to be compared
 * @param dest - the dest element whose attributes are to be compared
 * @param ignore - the string array of attributes whose values are to be ignored during the compare.
 * @return true if the attributes of both nodes meet the criteria of being equal, false otherwise.
 */
private static boolean compareAttributes(Element src, Element dest, String[] ignore) {
    NamedNodeMap srcAttrs = src.getAttributes();

    if (srcAttrs.getLength() != dest.getAttributes().getLength())
        return false;

    for (int ctr = 0; ctr < srcAttrs.getLength(); ctr++) {
        Node srcAttr = srcAttrs.item(ctr);

        String name = srcAttr.getNodeName();
        if (Arrays.binarySearch(ignore, name) < 0) {
            Node destAttr = dest.getAttributeNode(name);
            if (destAttr == null || !srcAttr.isEqualNode(destAttr)) {
                return false;
            }
        }
    }

    return true;
}

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 w w  w .j  a v a  2s  .  co m*/
    }
}

From source file:Main.java

public static Map<String, String> getAttributes(Node n) {
    if (n == null)
        return null;
    NamedNodeMap attributes = n.getAttributes();
    if (attributes == null || attributes.getLength() <= 0)
        return null;

    Map<String, String> result = new HashMap<>();
    for (int i = 0; i < attributes.getLength(); i++) {
        Attr attr = (Attr) attributes.item(i);
        result.put(attr.getNodeName(), attr.getNodeValue());
    }//from ww w  .j a va  2 s.c  o  m
    return result;
}

From source file:Main.java

public static Map<String, String> getAttrs(Element ele) {
    NamedNodeMap nodeMap = ele.getAttributes();
    Map<String, String> attrs = new HashMap<String, String>(nodeMap.getLength());
    for (int i = 0; i < nodeMap.getLength(); i++) {
        attrs.put(nodeMap.item(i).getNodeName(), nodeMap.item(i).getNodeValue());
    }//  www.j  av a 2s  .c o m
    return attrs;
}

From source file:Main.java

/**
 * Returns a map of the passed node's attributes
 * @param node The nopde to get an attribute map for
 * @return a [possibly empty] map of the node's attributes
 *//*from   w w  w  .j a v a  2  s.c  o  m*/
public static Map<String, String> getAttributeMap(final Node node) {
    if (node == null)
        throw new IllegalArgumentException("The passed node was null");
    final NamedNodeMap nnm = node.getAttributes();
    final int size = nnm.getLength();
    if (size == 0)
        return Collections.emptyMap();
    final Map<String, String> map = new LinkedHashMap<String, String>(size);
    for (int i = 0; i < size; i++) {
        final Attr attr = (Attr) nnm.item(i);
        map.put(attr.getName(), attr.getValue());
    }
    return map;
}

From source file:Main.java

public static String nodeToString(Node node) {
    String strNode = "";
    Node nodeIt = node;//from   w  w  w .  j  a  va2s  .c o  m

    if (nodeIt instanceof Element) {
        Element elem = (Element) nodeIt;
        String strElem = "";
        strElem += "<" + elem.getTagName();
        NamedNodeMap attribs = elem.getAttributes();
        int len = attribs.getLength();
        for (int i = 0; i < len; i++) {
            Attr attr = (Attr) attribs.item(i);
            strElem += " " + attr.getName() + "=\"" + attr.getValue() + "\"";
        }
        strElem += ">";
        strNode = strElem + strNode;
    } else if (nodeIt instanceof CharacterData) {
        CharacterData charNode = (CharacterData) nodeIt;
        strNode = charNode.getData() + strNode;
    } else if (nodeIt instanceof Document)
        strNode = strNode; // no hacemos nada
    else
        strNode = nodeIt.getNodeValue() + strNode;

    return strNode;
}

From source file:Main.java

/** Return the attributes from the specified element as a Map */
public static Map<String, String> getAttributesAsMap(Element e) {
    Map<String, String> result = new HashMap<String, String>();
    NamedNodeMap attrs = e.getAttributes();
    if (attrs != null) {
        int len = attrs.getLength();
        for (int i = 0; i < len; i++) {
            Node n = attrs.item(i);
            if (n instanceof Attr) {
                Attr a = (Attr) n;
                result.put(a.getName(), a.getValue());
            }/*  www  . j av a2 s .  co m*/
        }
    }
    return result;
}

From source file:Main.java

/**
 * Print all the attributes of the given node
 *
 * @param parent//www .  j a  v a2 s  .co  m
 */
public static void printNode(Node parent) {
    if (parent == null) {
        System.out.println("null node!");
        return;
    }
    System.out.println(parent.getNodeName() + " node:");
    NamedNodeMap attrs = parent.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attribute = (Attr) attrs.item(i);
        System.out.println("  " + attribute.getName() + " = " + attribute.getValue());
    }
}

From source file:Main.java

/**
 * @param element//from   w  w  w. j a v  a2  s . c  o  m
 * @param map
 * @return
 */
private static boolean fillAttsIntoMap(Element element, Map<String, String> map) {
    /*
     * this case is applicable only if there are no child elements
     */
    if (element.getChildNodes().getLength() > 0) {
        return false;
    }

    /*
     * in case this is not a special case, it will not have attributes, and
     * hence we are safe
     */
    NamedNodeMap attrs = element.getAttributes();
    int n = attrs.getLength();
    for (int i = 0; i < n; i++) {
        Node att = attrs.item(i);
        map.put(att.getNodeName(), att.getNodeValue());
    }
    return true;
}