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

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();
            }//  w  w w . j a va  2 s . c om

        }
    }

    return null;

}

From source file:Main.java

private static Map<String, Object> getAttributes(Node node) {
    NamedNodeMap attribs = node.getAttributes();
    int attribCount = attribs.getLength();
    Map<String, Object> map = new LinkedHashMap<>(attribCount);
    for (int j = 0; j < attribCount; j++) {
        Node attrib = attribs.item(j);
        map.put(attrib.getNodeName(), attrib.getNodeValue());
    }/*from w ww.j  a  va  2  s. c  o  m*/
    return map;
}

From source file:Main.java

/** Returns a sorted list of attributes.
 *
 *  @param attrs A map of attributes/*from   ww w .  ja v  a 2 s.co m*/
 *  @return A sorted array of attributes 
 */
protected static Attr[] sortAttributes(NamedNodeMap attrs) {
    int len = (attrs != null) ? attrs.getLength() : 0;
    Attr array[] = new Attr[len];
    for (int i = 0; i < len; i++) {
        array[i] = (Attr) attrs.item(i);
    }
    /*    
    for (int i = 0; i < len - 1; i++) {
      String name  = array[i].getNodeName();
      int index = i;
      for (int j = i + 1; j < len; j++) {
    String curName = array[j].getNodeName();
    if (curName.compareTo(name) < 0) {
      name  = curName;
      index = j;
    }
      }
      if (index != i) {
    Attr temp    = array[i];
    array[i]     = array[index];
    array[index] = temp;
      }
    }
     */
    return (array);
}

From source file:Main.java

/**
 * Indicates if the passed node has the named atribute
 * @param node The node to inspect/*from   w w w.j av a2 s.c om*/
 * @param name The name of the attribute to look for
 * @return true if the named attribute exists, false otherwise
 */
public static boolean hasAttribute(final Node node, final String name) {
    if (name == null || node == null)
        return false;
    final NamedNodeMap nnm = node.getAttributes();
    for (int i = 0; i < nnm.getLength(); i++) {
        Attr attr = (Attr) nnm.item(i);
        if (attr.getName().equalsIgnoreCase(name)) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

/**
 * Retrieves the number of Nodes in the NamedNodeMap
 * /* w w w.  j  a  va 2s  .c  o  m*/
 * @param nodeMap the NamedNodeMap
 * @return the number of Nodes
 **/
public final static int size(final NamedNodeMap nodeMap) {
    return nodeMap == null ? 0 : nodeMap.getLength();
}

From source file:Main.java

public static Node cloneNode(Document d, Node n) {
    Node r = null;/*from  ww  w  .  j a va  2 s . co  m*/
    switch (n.getNodeType()) {
    case Node.TEXT_NODE:
        r = d.createTextNode(((Text) n).getData());
        break;
    case Node.CDATA_SECTION_NODE:
        r = d.createCDATASection(((CDATASection) n).getData());
        break;
    case Node.ELEMENT_NODE:
        r = d.createElement(((Element) n).getTagName());
        NamedNodeMap map = n.getAttributes();
        for (int i = 0; i < map.getLength(); i++) {
            ((Element) r).setAttribute(((Attr) map.item(i)).getName(), ((Attr) map.item(i)).getValue());
        }
        break;
    }
    return r;
}

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  ww w . ja v a 2s. com*/
 *
 * @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

/**
 * Remove all attribute from the specified element
 */// w  ww .ja  v a  2  s.  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());
}

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   w  ww  .j  a  v a2  s .  c  om
    }
    return attrs;
}

From source file:Main.java

private static List<Node> filterOutIgnorableAttrs(NamedNodeMap map) {
    List<Node> list = new ArrayList<Node>();
    for (int i = 0; i < map.getLength(); i++) {
        Node attr = map.item(i);// w w w .  ja v  a2 s .  c om
        if (!attr.getNodeName().startsWith("xmlns") && !"xsi:type".equals(attr.getNodeName())) {
            list.add(attr);
        }
    }
    return list;
}