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 String nodeToString(Node node) {
    String ret = "<null/>";
    if (node != null) {
        ret = "<" + node.getNodeName() + " ";
        NamedNodeMap attrs = node.getAttributes();
        if (attrs != null) {
            for (int i = 0; i < attrs.getLength(); i++) {
                Node attr = (Node) attrs.item(i);
                ret = ret + attr.getNodeName() + "='";
                ret = ret + attr.getNodeValue() + "' ";
            }/*from  w  w  w  . j  a va2  s  .com*/
        }
        ret = ret + "/>";
    }
    return ret;
}

From source file:Main.java

/**
 * @return A String[] of length two, [prefix, URI].
 */// ww  w .  j  a v a 2s .  c om
public static String[] getNamespaceDeclaration(Element ele, String prefixHint) {
    String[] ns = new String[2]; // prefix, URI
    NamedNodeMap attribs = ele.getAttributes();

    for (int i = 0; i < attribs.getLength(); i++) {
        Attr attr = (Attr) attribs.item(i);
        if (attr.getName().startsWith("xmlns")) {
            if ((prefixHint != null && attr.getName().endsWith(prefixHint)) || attr.getName().equals("xmlns")) {
                ns[0] = attr.getLocalName(); // prefix
                ns[1] = attr.getTextContent(); // URI

                // catch default namespace change
                if (ns[0] == "xmlns") {
                    ns[0] = UUID.randomUUID().toString();
                }
            }
        }
    }

    if (ns[1] == null) {
        return getNamespaceDeclaration((Element) ele.getParentNode(), prefixHint);
    } else {
        return ns;
    }
}

From source file:Main.java

/**
 * Finds attribute of node by name.//from  ww  w.j av a  2  s .c om
 * @param node Node The context node.
 * @param name String
 * @return Node
 */
public static Node findAttribute(Node node, String name) {
    Node found = null;
    NamedNodeMap nm = node.getAttributes();
    if (nm != null) {
        for (int i = 0; i < nm.getLength(); i++) {
            Node attr = nm.item(i);
            if (attr.getNodeName().compareToIgnoreCase(name) == 0) {
                found = attr;
                break;
            }
        }
    }
    return found;
}

From source file:Main.java

/**
 * Convert a {@link NamedNodeMap} contains attribute nodes {@link Attr}
 *
 * @param attrList {@link NamedNodeMap} containing {@link Attr} objects exclusively
 * @return List of {@link Attr}.// w ww.  j  a va 2s  .c  om
 */
public static List<Attr> toList(NamedNodeMap attrList) {
    ArrayList<Attr> list = new ArrayList<Attr>(attrList.getLength());
    for (int i = 0; i < attrList.getLength(); i++) {
        list.add((Attr) attrList.item(i));
    }
    return list;
}

From source file:Main.java

/** Return the attributes from the specified element as a Map */
public static Map<String, String> getAttributesAsMap(Element e) {
    Map<String, String> result = new HashMap<String, String>();
    NamedNodeMap attrs = e.getAttributes();
    if (attrs != null) {
        int len = attrs.getLength();
        for (int i = 0; i < len; i++) {
            Node n = attrs.item(i);
            if (n instanceof Attr) {
                Attr a = (Attr) n;
                result.put(a.getName(), a.getValue());
            }//w  ww .  j  av a2s .c  o m
        }
    }
    return result;
}

From source file:Main.java

public static String getPrefix(String uri, Node e) {
    while (e != null && (e.getNodeType() == Element.ELEMENT_NODE)) {
        NamedNodeMap attrs = e.getAttributes();
        for (int n = 0; n < attrs.getLength(); n++) {
            Attr a = (Attr) attrs.item(n);
            String name;//from   w w w .j av  a  2 s .  com
            if ((name = a.getName()).startsWith("xmlns:") && a.getNodeValue().equals(uri)) {
                return name.substring(6);
            }
        }
        e = e.getParentNode();
    }
    return null;
}

From source file:Main.java

@SuppressWarnings("fallthrough")
private static void getSetRec(final Node rootNode, final Set<Node> result, final Node exclude,
        final boolean com) {
    if (rootNode == exclude) {
        return;/*  w  w w .  j  av  a 2 s .c om*/
    }
    switch (rootNode.getNodeType()) {
    case Node.ELEMENT_NODE:
        result.add(rootNode);
        Element el = (Element) rootNode;
        if (el.hasAttributes()) {
            NamedNodeMap nl = el.getAttributes();
            for (int i = 0; i < nl.getLength(); i++) {
                result.add(nl.item(i));
            }
        }
        //no return keep working
    case Node.DOCUMENT_NODE:
        for (Node r = rootNode.getFirstChild(); r != null; r = r.getNextSibling()) {
            if (r.getNodeType() == Node.TEXT_NODE) {
                result.add(r);
                while (r != null && r.getNodeType() == Node.TEXT_NODE) {
                    r = r.getNextSibling();
                }
                if (r == null) {
                    return;
                }
            }
            getSetRec(r, result, exclude, com);
        }
        return;
    case Node.COMMENT_NODE:
        if (com) {
            result.add(rootNode);
        }
        return;
    case Node.DOCUMENT_TYPE_NODE:
        return;
    default:
        result.add(rootNode);
    }
}

From source file:Main.java

/**
 * Gather all the namespaces defined on a node
 *
 * @return//w w w  .j ava 2 s .c om
 */
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 Map<String, String> getNamespaceMap(Document document) {
    Map<String, String> nsMap = new HashMap<String, String>();

    NamedNodeMap map = document.getDocumentElement().getAttributes();
    for (int j = 0; j < map.getLength(); j++) {
        Node n = map.item(j);
        String attrName = ((Attr) n).getName();
        String attrValue = ((Attr) n).getValue();
        if (attrName != null && attrValue != null) {
            if (attrName.trim().startsWith("xmlns:")) {
                nsMap.put(attrValue, attrName.substring(6));
            }//ww  w.jav  a 2 s  .co  m
        }
    }

    return nsMap;
}

From source file:Main.java

/**
 * Gets a attribute value.//from   w  ww .  j a  v  a  2  s  .c  om
 *
 * @param pNode
 *            the p node
 * @param pAttributeName
 *            the p attribute name
 * @return the attribute value
 */
public static String getAttributeValue(Node pNode, String pAttributeName) {
    String returnString = null;

    NamedNodeMap attributes = pNode.getAttributes();
    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            if (attributes.item(i).getNodeName().equalsIgnoreCase(pAttributeName)) {
                returnString = attributes.item(i).getNodeValue();
                break;
            }
        }
    }

    return returnString;
}