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

static public 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 . ja  v  a  2 s  . c om*/
    }
    return "";
}

From source file:Main.java

/**
 * Dumps a debug listing of the attributes of the given element to
 * System.out.//www  .ja  v a  2s . c o  m
 * 
 * @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

/**
 * Converts {@link NamedNodeMap} to a {@link LinkedHashMap}&lt;String,String&gt;.
 * @param _nodeMap node map/*w w w . j a v a2s. c o m*/
 * @return {@link LinkedHashMap}, maybe empty but never null
 */
public static Map<String, String> convertToAttributeMap(NamedNodeMap _nodeMap) {
    Map<String, String> map = new LinkedHashMap<>();
    for (int i = 0; i < _nodeMap.getLength(); i++) {
        Node node = _nodeMap.item(i);
        map.put(node.getNodeName(), node.getNodeValue());
    }
    return map;
}

From source file:Main.java

public static Properties getElementAttributes(Element ele) {
    Properties ht = new Properties();
    NamedNodeMap nnm = ele.getAttributes();
    int len = nnm.getLength();
    Node tmpn = null;//from  w  w w.  ja  v a  2s .  c o m
    for (int k = 0; k < len; k++) {
        tmpn = nnm.item(k);
        String tmps = tmpn.getNodeValue();
        if (tmps == null) {
            tmps = "";
        }
        ht.put(tmpn.getNodeName(), tmps);
    }
    return ht;
}

From source file:Main.java

/**
 * Returns formatted attributes of the node.
 * // w w  w  .  ja  v a  2s .  c  om
 * @param node The node.
 * @return Formatted attributes.
 */
public static String formatAttributes(Node node) {
    StringBuilder sb = new StringBuilder();
    NamedNodeMap attrs = node.getAttributes();

    for (int i = 0; i < attrs.getLength(); i++) {
        Node attr = attrs.item(i);
        sb.append(' ').append(attr.getNodeName()).append("= '").append(attr.getNodeValue()).append("'");
    }

    return sb.toString();
}

From source file:Main.java

public static HashMap<String, String> getEleAttrNameValueMap(Element ele) {
    HashMap<String, String> ht = new HashMap<String, String>();
    NamedNodeMap nnm = ele.getAttributes();
    int len = nnm.getLength();
    Node tmpn = null;/*from w w  w . j a v  a 2s .com*/
    for (int k = 0; k < len; k++) {
        tmpn = nnm.item(k);
        String tmps = tmpn.getNodeValue();
        if (tmps == null) {
            tmps = "";
        }
        ht.put(tmpn.getNodeName(), tmps);
    }
    return ht;
}

From source file:Main.java

/**
 * Print all the attributes of the given node
 *
 * @param parent/*from ww w .j av a  2 s. c o 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:Utils.java

public static String getPrefix(Element el, String ns) {
    NamedNodeMap atts = el.getAttributes();
    for (int i = 0; i < atts.getLength(); i++) {
        Node node = atts.item(i);
        String name = node.getNodeName();
        if (ns.equals(node.getNodeValue())
                && (name != null && (XMLNAMESPACE.equals(name) || name.startsWith(XMLNAMESPACE + ":")))) {
            return node.getPrefix();
        }//w  ww. ja  va2 s .  c o m
    }
    return null;
}

From source file:Main.java

private static void outputElement(Element node, String indent) {
    System.out.print(indent + "<" + node.getTagName());
    NamedNodeMap nm = node.getAttributes();
    for (int i = 0; i < nm.getLength(); i++) {
        Attr attr = (Attr) nm.item(i);
        System.out.print(" " + attr.getName() + "=\"" + attr.getValue() + "\"");
    }//from w  w  w.  j ava2 s  .  c o  m
    System.out.print(">");
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++)
        outputloop(list.item(i), indent + TAB);
    System.out.println(indent + "</" + node.getTagName() + ">");
}

From source file:Main.java

public static void removeInvalidAttributes(Element element, String... validAttributeNames) {
    Set<String> validNames = new HashSet<String>();
    for (String name : validAttributeNames) {
        validNames.add(name);/*www . j  a  va  2 s .  c o  m*/
    }
    boolean elementModified = false;
    NamedNodeMap nnm = element.getAttributes();
    for (int i = 0; i < nnm.getLength(); i++) {
        String attributeName = nnm.item(i).getNodeName();
        if (!validNames.contains(attributeName)) {
            element.removeAttribute(attributeName);
            elementModified = true;
        }
    }
    if (elementModified) {
        flagDocumentAsCorrected(element);
    }
}