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

public static Node duplicate(Node node, Document ownerDocument) {
    if (node instanceof Text)
        return ownerDocument.createTextNode(node.getNodeValue());
    Node newNode = ownerDocument.createElement(node.getNodeName());
    NamedNodeMap attribs = node.getAttributes();
    for (int i = 0; i < attribs.getLength(); i++) {
        Node attrib = attribs.item(i);
        addAttribute(newNode, attrib.getNodeName(), attrib.getNodeValue());
    }//from  w ww. ja  va 2 s. co m
    for (Node n = node.getFirstChild(); n != null; n = n.getNextSibling()) {
        Node newN = duplicate(n, ownerDocument);
        newNode.appendChild(newN);
    }
    return newNode;
}

From source file:Main.java

public static void printAttributes(Element element) {
    NamedNodeMap attributes = element.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Node node = attributes.item(i);
        System.err.println("## prefix=" + node.getPrefix() + " localname:" + node.getLocalName() + " value="
                + node.getNodeValue());/* ww w  . j  a v  a  2  s . c o m*/
    }
}

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

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

From source file:Main.java

/***
 *  Print every attribute and value of a node, one line at a time.
 *  If {@link entry} is null, then outputs "&lt;null/&gt;".
 *
 * @param outs where to send output, cannot be null.
 * @param entry XML node to examine, OK if null.
 *///  w  w  w.  j ava2 s  . co m
public static void XmlPrintAttrs(PrintStream outs, Node entry) {
    if (entry == null) {
        outs.print("<null/>");
        return;
    }

    //  see http://www.w3.org/2003/01/dom2-javadoc/org/w3c/dom/NamedNodeMap.html
    NamedNodeMap attrs = entry.getAttributes();

    for (int k = attrs.getLength(); --k >= 0;) {
        Node n = attrs.item(k);
        outs.printf("+++ has attr %s = %s\n", n.getNodeName(), n.getNodeValue());
    }
}

From source file:Main.java

/** Remove stray "xmlns" default namespace element that seems to get left over even after removing namespacing from nodes.
 * @param node node to process/*from   w  ww .j  a  v a 2  s  .c o  m*/
 */
public static void removeXmlNsAttribute(final Node node) {
    final NamedNodeMap attr = node.getAttributes();
    for (int i = 0; i < attr.getLength(); i++) {
        final Node item = attr.item(i);
        if (ATTR_XMLNS.equals(item.getNodeName())) {
            attr.removeNamedItem(ATTR_XMLNS);
            return;
        }
    }
}

From source file:Main.java

public static List<Attr> getAttributes(Element element) {

    List<Attr> attributes = new LinkedList<Attr>();

    NamedNodeMap namedNodeMap = element.getAttributes();

    for (int i = 0; i < namedNodeMap.getLength(); i++) {
        Node node = namedNodeMap.item(i);
        if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
            Attr a = (Attr) node;
            attributes.add(a);/*from   www . ja  va2  s .  co m*/
        }
    }

    return attributes;
}

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);
        if (!attr.getNodeName().startsWith("xmlns") && !"xsi:type".equals(attr.getNodeName())) {
            list.add(attr);//from   w  w  w.  j a v a 2s. c  o m
        }
    }
    return list;
}

From source file:Main.java

public static void removeAttributes(Element element) {
    NamedNodeMap namedNodeMap = element.getAttributes();
    while (namedNodeMap.getLength() > 0) {
        element.removeAttributeNode((Attr) namedNodeMap.item(0));
    }//from  ww  w  .  j a  v  a 2  s.  c o  m
}

From source file:Main.java

/**
 * Indicates if the passed node has the named atribute
 * @param node The node to inspect/* w  ww  . j a  v  a 2 s .  com*/
 * @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

public static String getNamespaceUriDeclaration(Element ele) {
    NamedNodeMap attribs = ele.getAttributes();

    for (int i = 0; i < attribs.getLength(); i++) {
        Attr attr = (Attr) attribs.item(i);
        if ("xmlns".equals(attr.getLocalName()) || XMLConstants.XML_NS_URI.equals(attr.getNamespaceURI())) {
            return attr.getTextContent();
        }/* www . ja v  a2 s .  c o  m*/
    }

    return "";
}