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 Map<String, String> getAttrList(Node node) {
    Map<String, String> map = new HashMap<String, String>();

    NamedNodeMap list = node.getAttributes();
    if (list != null)
        for (int i = 0; i < list.getLength(); i++) {
            Attr attr = (Attr) list.item(i);
            map.put(attr.getName(), attr.getValue());
        }/*from w w  w .j  a  va  2  s . c  o m*/

    return map;
}

From source file:Main.java

private static boolean hasEqualAttributes(Node arg0, Node arg) {

    NamedNodeMap map1 = arg0.getAttributes();
    NamedNodeMap map2 = arg.getAttributes();
    int len = map1.getLength();
    if (len != map2.getLength()) {
        return false;
    }//from   w  ww  .j  a v  a  2s .  com

    for (int i = 0; i < len; i++) {
        Node n1 = map1.item(i);
        if (n1.getNodeName() != null) {
            Node n2 = map2.getNamedItem(n1.getNodeName());
            if (n2 == null) {
                return false;
            } else if (!n1.getNodeValue().equals(n2.getNodeValue())) {
                return false;
            }
        }
    }
    return true;
}

From source file:Main.java

public static void removeNamespaceDeclarations(Node node, String... namespaces) {
    NamedNodeMap namedNodeMap = ((Element) node).getAttributes();
    for (int nameIndex = 0; nameIndex < namedNodeMap.getLength(); nameIndex++) {
        Node namedNode = namedNodeMap.item(nameIndex);
        String uri = namedNode.getNamespaceURI();
        String localName = namedNode.getLocalName();
        if (uri != null && uri.equals("http://www.w3.org/2000/xmlns/")) {
            for (String removeableNamespace : namespaces) {
                if (namedNode.getNodeValue().equals(removeableNamespace)) {
                    ((Element) node).removeAttributeNS("http://www.w3.org/2000/xmlns/", localName);
                    nameIndex--;//from   ww w  .  j ava  2s .  c om
                }
            }
        }
    }
}

From source file:MainClass.java

public static void print(Node node, OutputStream os) {
    PrintStream ps = new PrintStream(os);
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        ps.print("<" + node.getNodeName());

        NamedNodeMap map = node.getAttributes();
        for (int i = 0; i < map.getLength(); i++) {
            ps.print(" " + map.item(i).getNodeName() + "=\"" + map.item(i).getNodeValue() + "\"");
        }/*from   www .  j  ava 2 s .c o m*/
        ps.println(">");
        return;
    case Node.ATTRIBUTE_NODE:
        ps.println(node.getNodeName() + "=\"" + node.getNodeValue() + "\"");
        return;
    case Node.TEXT_NODE:
        ps.println(node.getNodeValue());
        return;
    case Node.CDATA_SECTION_NODE:
        ps.println(node.getNodeValue());
        return;
    case Node.PROCESSING_INSTRUCTION_NODE:
        ps.println(node.getNodeValue());
        return;
    case Node.DOCUMENT_NODE:
    case Node.DOCUMENT_FRAGMENT_NODE:
        ps.println(node.getNodeName() + "=" + node.getNodeValue());
        return;
    }
}

From source file:Main.java

/**
 * Gather all the namespaces defined on a node
 *
 * @return/*from   w ww .ja v  a 2s  .  c o  m*/
 */
public static Iterable<Entry<String, String>> getNamespaces(Element element) {
    TreeMap<String, String> map = new TreeMap<String, String>();
    do {
        NamedNodeMap attributes = element.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Attr attr = (Attr) attributes.item(i);
            final String name = attr.getLocalName();

            if (attr.getPrefix() != null) {
                if ("xmlns".equals(attr.getPrefix()))
                    if (!map.containsKey(name))
                        map.put(name, attr.getValue());
            } else if ("xmlns".equals(name)) {
                if (!map.containsKey(""))
                    map.put("", attr.getValue());
            }
        }
        if (element.getParentNode() == null || element.getParentNode().getNodeType() != Node.ELEMENT_NODE)
            break;
        element = (Element) element.getParentNode();
    } while (true);
    return map.entrySet();
}

From source file:Main.java

public static String nodeToString(Node node) {
    String s = node.getNodeName() + "( ";
    NamedNodeMap map = node.getAttributes();
    if (map != null) {
        for (int i = 0; i < map.getLength(); i++) {
            s += map.item(i).getNodeName() + "=" + map.item(i).getNodeValue() + " ";
        }/*from   w w  w . j  a  va  2 s .  c  o  m*/
    }
    return s += " )";
}

From source file:Main.java

private static Map<?, ?> getNodeBean(Node parent) {
    Map<Object, Object> rtn = new HashMap<Object, Object>();

    if (parent != null) {
        Map<Object, Object> attrMap = new HashMap<Object, Object>();
        if (parent.hasAttributes()) {
            NamedNodeMap attrs = parent.getAttributes();
            for (int j = 0; j < attrs.getLength(); j++) {
                Node attr = attrs.item(j);
                attr.getNodeName();//from   ww  w  . j a  v a  2  s  .com
                attr.getNodeValue();
                attrMap.put(attr.getNodeName(), attr.getNodeValue());
            }
        }

        rtn.put("tagName", parent.getNodeName());
        rtn.put("attr", attrMap);

        NodeList nodeList = parent.getChildNodes();
        if (nodeList != null) {
            List<Object> children = new ArrayList<Object>();
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node child = nodeList.item(i);
                if (child.getNodeType() == Node.ELEMENT_NODE) {
                    children.add(getNodeBean(child));
                }
            }

            rtn.put("children", children);
        }
    }

    return rtn;
}

From source file:Main.java

protected static Map<String, String> createAttributeTable(Element e) {
    Map<String, String> attributeTable = new HashMap<>();
    NamedNodeMap attrs = e.getAttributes();
    if (attrs != null) {
        int numAttrs = attrs.getLength();
        for (int i = 0; i < numAttrs; i++) {
            Node na = attrs.item(i);
            if (na.getNodeType() != Node.ATTRIBUTE_NODE) {
                continue;
            }//from   www.  j a va 2  s  . c  o  m
            Attr a = (Attr) na;
            attributeTable.put(a.getName(), a.getValue());
        }
    }
    return attributeTable;
}

From source file:Main.java

/**
 * Build {@link Map} of namespace URIs to prefixes.
 * //from   w w w  .j av  a  2  s.  co  m
 * @param root
 *          {@link Element} to get namespaces and prefixes from.
 * @return {@link Map} of namespace URIs to prefixes.
 * @since 8.1
 */
private static final Map<String, String> buildNamespacePrefixMap(final Element root) {
    final HashMap<String, String> namespacePrefixMap = new HashMap<>();

    //Look for all of the attributes of cache that start with
    //xmlns
    NamedNodeMap attributes = root.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Node item = attributes.item(i);
        if (item.getNodeName().startsWith("xmlns")) {
            //Anything after the colon is the prefix
            //eg xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            //has a prefix of xsi
            String[] splitName = item.getNodeName().split(":");
            String prefix;
            if (splitName.length > 1) {
                prefix = splitName[1];
            } else {
                prefix = "";
            }
            String uri = item.getTextContent();
            namespacePrefixMap.put(uri, prefix);
        }
    }

    return namespacePrefixMap;
}

From source file:Main.java

public static String findPrefixForNamespace(Element elm, String namespace) {
    while (elm != null) {
        NamedNodeMap attributes = elm.getAttributes();
        for (int c = 0; c < attributes.getLength(); c++) {
            if (attributes.item(c).getNodeValue().equals(namespace)
                    && attributes.item(c).getNodeName().startsWith("xmlns:")) {
                return attributes.item(c).getNodeName().substring(6);
            }/*  www. j a  v  a2 s  .c  o m*/
        }

        if (elm.getParentNode().getNodeType() != Node.ELEMENT_NODE)
            break;

        elm = (Element) elm.getParentNode();
    }

    return null;
}