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

private static void addAttrDirect(Map<String, Object> values, Element el) {
    NamedNodeMap attrs = el.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Node attr = attrs.item(i);
        values.put(attr.getNodeName(), attr.getNodeValue());
    }/* ww  w  .  j  a  va 2  s.c  o  m*/
}

From source file:Main.java

/**
 * Dumps a debug listing of the attributes of the given element to
 * System.out.//from w  w w  . j  a va2s  .  com
 * 
 * @param elem the element to dump the attributes of
 */
public static void dumpAttrs(Element elem) {
    System.out.println("Attributes of " + elem.getNodeName() + ", NS: " + elem.getNamespaceURI());
    NamedNodeMap attrs = elem.getAttributes();
    int len = attrs.getLength();
    for (int i = 0; i < len; ++i) {
        Node attr = attrs.item(i);
        System.out.println("  Name: " + attr.getNodeName() + ", Value: " + attr.getNodeValue() + ", NS: "
                + attr.getNamespaceURI());
    }
}

From source file:Main.java

public static void removeNodeAttributes(Node node) {

    NamedNodeMap attrs = node.getAttributes();

    if ((attrs != null) && (attrs.getLength() > 0)) {
        String[] names = new String[attrs.getLength()];
        for (int i = 0; i < names.length; i++) {
            names[i] = attrs.item(i).getNodeName();
        }/*www.j av a  2s  . c o  m*/
        for (int i = 0; i < names.length; i++) {
            attrs.removeNamedItem(names[i]);
        }
    }

}

From source file:Main.java

public static List<Node> getAttributes(Node node) {
    NamedNodeMap attrs = node.getAttributes();
    int attrCount = attrs.getLength();
    List<Node> nodes = new ArrayList<Node>(attrCount);
    for (int i = 0; i < attrCount; i++) {
        nodes.add(attrs.item(i));//from  ww w .j a  va 2 s . c o m
    }
    return nodes;
}

From source file:Main.java

/**
 * Gets the names of all the attributes of the given {@link Element}.
 *
 * @param element The {@link Element} to get the attribute names of.
 * @return Returns an array, possibly empty, of the attribute names.
 *///from  w w w  .jav a 2  s  . com
public static String[] getAttributesNamesOf(Element element) {
    final NamedNodeMap map = element.getAttributes();
    final int numAttributes = map.getLength();
    final String[] result = new String[numAttributes];
    for (int i = 0; i < numAttributes; ++i)
        result[i] = map.item(i).getNodeName();
    return result;
}

From source file:Main.java

public static List<String> getAllAttrValueByName(Node item, String name) {
    List<String> lst = null;
    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();
        if (name.equals(attrName)) {
            if (lst == null) {
                lst = new ArrayList();
            }//from   w ww  .  ja  v  a 2 s.  c  o  m
            lst.add(attr.getNodeValue());
        }
    }
    return lst;
}

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 {//from   ww  w . j  av a  2  s. co m
            if (name.equals(attrName)) {
                return attr.getNodeValue();
            }
        }
    }
    return null;
}

From source file:Main.java

public static void stepThrough(Node start) {
    System.out.println(start.getNodeName() + " = " + start.getNodeValue());

    if (start.getNodeType() == Element.ELEMENT_NODE) {
        NamedNodeMap startAttr = start.getAttributes();
        for (int i = 0; i < startAttr.getLength(); i++) {
            Node attr = startAttr.item(i);
            System.out.println(" Attribute: " + attr.getNodeName() + " = " + attr.getNodeValue());
        }//from   w w  w.java2s .c  o  m
    }

    for (Node child = start.getFirstChild(); child != null; child = child.getNextSibling()) {
        stepThrough(child);
    }
}

From source file:Main.java

private static void serializeAttributeOf(XmlSerializer serializer, Element element) throws IOException {
    NamedNodeMap map = element.getAttributes();
    for (int i = 0; i < map.getLength(); i++) {
        Node attr = map.item(i);/*from  w w w.java  2 s .co  m*/
        serializer.attribute(null, attr.getNodeName(), attr.getNodeValue());
    }
}

From source file:Main.java

/**
 * Convert a {@link NamedNodeMap} contains attribute nodes {@link Attr}
 *
 * @param attrList {@link NamedNodeMap} containing {@link Attr} objects exclusively
 * @return List of {@link Attr}./*from ww  w.j a  v a2 s.c  om*/
 */
public static List<Attr> toList(NamedNodeMap attrList) {
    ArrayList<Attr> list = new ArrayList<Attr>(attrList.getLength());
    for (int i = 0; i < attrList.getLength(); i++) {
        list.add((Attr) attrList.item(i));
    }
    return list;
}