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 Attr[] getAttrs(Element elem) {
    NamedNodeMap attrMap = elem.getAttributes();
    Attr[] attrArray = new Attr[attrMap.getLength()];
    for (int i = 0; i < attrMap.getLength(); i++)
        attrArray[i] = (Attr) attrMap.item(i);
    return attrArray;
}

From source file:Main.java

/**
 * Returns all attributes of the given XML node as a list of name=value strings.
 * Mostly just for debugging./*w w  w  .  ja  v a  2 s.  c o  m*/
 * @param node
 * @return
 */
public static List<String> getAttributes(Node node) {
    List<String> result = new ArrayList<String>();
    NamedNodeMap attrs = node.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Node attrNode = attrs.item(i);
        result.add(attrNode.getNodeName() + "=" + attrNode.getNodeValue());
    }
    return result;
}

From source file:Main.java

public static String getAttribute(Node pNode, String attrName) {
    try {/*w  w  w.  j  a  v  a  2  s .  c  om*/
        NamedNodeMap name = pNode.getAttributes();
        if (name.getLength() > 0) {
            Node node = name.getNamedItem(attrName);
            if (node != null) {
                String attributeValue = name.getNamedItem(attrName).getNodeValue();
                return attributeValue;
            } else {
                return null;
            }
        } else {
            return null;
        }
    } catch (Exception e) {
        return null;
    }

}

From source file:Main.java

private static void traverseAttributes(JsonObject childJson, Node childNode) {
    NamedNodeMap attrNodeMap = childNode.getAttributes();
    for (int j = 0; j < attrNodeMap.getLength(); j++) {
        Node attrNode = attrNodeMap.item(j);
        childJson.addProperty("-" + attrNode.getNodeName(), attrNode.getNodeValue());
    }//ww w.j  a  va  2  s  .c  om
}

From source file:Main.java

public static void getNamespaces(Node node, Map<String, String> list) {
    NamedNodeMap atts = node.getAttributes();
    for (int i = 0; i < atts.getLength(); i++) {
        Node n = atts.item(i);//from w w w .j  av  a2 s  .  c om
        if ("xmlns".equals(n.getNodeName())) {
            list.put(n.getNodeName(), n.getNodeValue());
        } else {
            if (n.getNodeName().startsWith("xmlns:")) {
                list.put(n.getNodeName().substring(6), n.getNodeValue());
            }
        }
    }
}

From source file:Main.java

private static int countNonNamespaceAttribures(NamedNodeMap attrs) {
    int n = 0;/* w  ww.j av a2  s.c  o  m*/
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr = (Attr) attrs.item(i);
        if (!attr.getName().startsWith("xmlns")) {
            n++;
        }
    }
    return n;
}

From source file:Main.java

public static HashMap<String, String> getNodeAttributesMap(Node node) {
    HashMap<String, String> map = new HashMap<String, String>();

    NamedNodeMap namedNodeMap = node.getAttributes();
    for (int i = 0; i < namedNodeMap.getLength(); i++) {
        Node attribute = namedNodeMap.item(i);
        map.put(attribute.getNodeName(), attribute.getNodeValue());
    }// w  ww . j  a v  a 2  s  .c  om

    return map;
}

From source file:Main.java

public static String getAttributeValue(Element start, String attrName) {
    if (start.getNodeType() == Element.ELEMENT_NODE) {
        NamedNodeMap startAttr = start.getAttributes();
        for (int i = 0; i < startAttr.getLength(); i++) {
            Node attr = startAttr.item(i);
            if (attrName.equals(attr.getNodeName().trim()))
                return attr.getNodeValue().trim();
        }/*from   w  w  w  .j a  v  a 2  s  .c o m*/
        return null;
    } else
        return null;
}

From source file:Main.java

private static void transferAttributes(Element elm, Element elm2) {
    NamedNodeMap attributes = elm.getAttributes();
    for (int c = 0; c < attributes.getLength(); c++) {
        Attr attr = (Attr) attributes.item(c);
        elm2.setAttributeNodeNS((Attr) elm2.getOwnerDocument().importNode(attr, true));
    }//from w w  w  .j  a v a  2s. com
}

From source file:Main.java

public static String lookupNamespaceURI(Node root, String specifiedPrefix) {
    if (root == null) {
        return null;
    }//from w w w  . j  av a 2  s  .  co m
    if (root.hasAttributes()) {
        NamedNodeMap nnm = root.getAttributes();
        for (int i = 0; i < nnm.getLength(); i++) {
            Node n = nnm.item(i);
            if (("xmlns".equals(n.getPrefix()) && specifiedPrefix.equals(n.getNodeName()))
                    || ("xmlns:" + specifiedPrefix).equals(n.getNodeName())) {
                return n.getNodeValue();
            }
        }
    }
    return lookupNamespaceURI(root.getParentNode(), specifiedPrefix);
}