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 getNamespaceURI(Document doc, String ns) {
    NamedNodeMap attr = doc.getDocumentElement().getAttributes();
    for (int i = 0; i < attr.getLength(); i++) {
        Node attrNode = attr.item(i);
        if (attrNode.getNodeName().indexOf(ns) != -1) {
            return (attrNode.getNodeValue());
        }//from w  w  w  .j  av a2  s. c  om
    }
    return null;
}

From source file:Main.java

public static Node findByID(Document doc, String id, String tagName) {
    NodeList node = doc.getElementsByTagName(tagName);
    for (int i = 0; i < node.getLength(); i++) {
        NamedNodeMap attributtes = node.item(i).getAttributes();
        for (int j = 0; j < attributtes.getLength(); j++)
            if (id.equalsIgnoreCase(attributtes.item(j).getNodeValue()))
                return node.item(i);
    }//from  w  w w .  j  a  v  a  2 s .c  o  m

    return null;
}

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());//  w w w.  ja v  a 2 s . co m
    }
}

From source file:Main.java

public static String getNamespaceURI(final Document doc, final String ns) {
    NamedNodeMap attr = doc.getDocumentElement().getAttributes();
    for (int i = 0; i < attr.getLength(); i++) {
        Node attrNode = attr.item(i);
        if (attrNode.getNodeName().indexOf(ns) != -1) {
            return (attrNode.getNodeValue());
        }//from   w ww .j av a2 s .  c o m
    }
    return null;
}

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());
    }/*  www . jav  a 2  s.  c  o  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 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   w ww .j  a v a 2 s  . c om
        }
    }

    return attributes;
}

From source file:Main.java

private static String unevaluatedXML(String result, Node node) {
    String nodeName = node.getNodeName();
    String attributes = "";
    if (node.hasAttributes()) {
        NamedNodeMap XMLAttributes = node.getAttributes();
        for (int i = 0; i < XMLAttributes.getLength(); i++)

        {/*from   w ww  .  j  a  v  a  2 s  .  c  o  m*/
            attributes += " " + XMLAttributes.item(i).getNodeName() + "=\""
                    + XMLAttributes.item(i).getNodeValue() + "\"";
        }
    }
    if (result.equals(""))
        return " <" + nodeName + attributes + "/> ";
    else
        return " <" + nodeName + attributes + ">" + result + "</" + nodeName + "> "; // add spaces
}

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  w  w  .  jav  a 2  s.com*/
 */
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 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();
        }//  w w w.j  a  va2  s.co m
    }

    return "";
}

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.
 *///from   w  ww .  j a  v  a 2 s.c o 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());
    }
}