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:Utils.java

/**
 * Get all prefixes defined on this element for the specified namespace.
 * //w w w.  ja  v a  2  s.c o  m
 * @param element
 * @param namespaceUri
 * @param prefixes
 */
public static void getPrefixes(Element element, String namespaceUri, List<String> prefixes) {
    NamedNodeMap atts = element.getAttributes();
    for (int i = 0; i < atts.getLength(); i++) {
        Node node = atts.item(i);
        String name = node.getNodeName();
        if (namespaceUri.equals(node.getNodeValue())
                && (name != null && (XMLNAMESPACE.equals(name) || name.startsWith(XMLNAMESPACE + ":")))) {
            prefixes.add(node.getPrefix());
        }
    }
}

From source file:Main.java

public static Node cloneNode(Document d, Node n) {
    Node r = null;/*from   w  ww.  j  a v  a  2  s.co  m*/
    switch (n.getNodeType()) {
    case Node.TEXT_NODE:
        r = d.createTextNode(((Text) n).getData());
        break;
    case Node.CDATA_SECTION_NODE:
        r = d.createCDATASection(((CDATASection) n).getData());
        break;
    case Node.ELEMENT_NODE:
        r = d.createElement(((Element) n).getTagName());
        NamedNodeMap map = n.getAttributes();
        for (int i = 0; i < map.getLength(); i++) {
            ((Element) r).setAttribute(((Attr) map.item(i)).getName(), ((Attr) map.item(i)).getValue());
        }
        break;
    }
    return r;
}

From source file:Main.java

/**
 * * Convenience method to transfer a node (and all of its children) from one
 * * DOM XML document to another./*from   w w w. j  ava 2  s . c om*/
 * *
 * * Note: this method is recursive.
 * *
 * * @param current the current Element to append the transfer to
 * * @param target the target document for the transfer
 * * @param n the Node to transfer
 * * @return Element the current element.
 */
public static Element transferNode(Element current, Document target, Node n) {
    String name = n.getNodeName();
    String value = n.getNodeValue();
    short type = n.getNodeType();

    if (type == Node.ELEMENT_NODE) {
        // create a new element for this node in the target document
        Element e = target.createElement(name);

        // move all the attributes over to the target document
        NamedNodeMap attrs = n.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Node a = attrs.item(i);
            e.setAttribute(a.getNodeName(), a.getNodeValue());
        }

        // get the children for this node
        NodeList children = n.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            // transfer each of the children to the new document
            transferNode(e, target, children.item(i));
        }

        // append the node to the target document element
        current.appendChild(e);
    } else if (type == Node.TEXT_NODE) {
        Text text = target.createTextNode(value);
        current.appendChild(text);
    }

    return current;
}

From source file:Main.java

/**
 * @param n Node to examine//from  ww w  .j  av  a 2  s  .  c o  m
 * @param attr Attribute to look for
 * @param def Default value to return if attribute is not present
 * @return if the Node contains the named Attribute, the value, if not, the def parameter
 */
public static String getAttributeIgnoreCase(Node n, String attr, String def) {
    NamedNodeMap attrs = n.getAttributes();
    if (attrs == null)
        return def;
    for (int i = 0; i < attrs.getLength(); i++) {
        Node ret = attrs.item(i);
        if (ret.getNodeName().equalsIgnoreCase(attr))
            return ret.getNodeValue();
    }
    return def;
}

From source file:Main.java

public static Properties extractProperties(Node node) {
    Properties props = new Properties();
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            Node item = attributes.item(i);
            props.put(item.getNodeName(), item.getNodeValue());
        }//from  w  w  w.ja v  a 2s  .  co  m
    }
    return props;
}

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 w  w. j a v  a  2 s  . c om*/
            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

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

From source file:Main.java

public static void printTree(Node doc) {
    if (doc == null) {
        System.out.println("Nothing to print!!");
        return;//w ww . ja  v  a 2  s.  c o  m
    }
    try {
        System.out.println(doc.getNodeName() + "  " + doc.getNodeValue());
        NamedNodeMap cl = doc.getAttributes();
        for (int i = 0; i < cl.getLength(); i++) {
            Node node = cl.item(i);
            System.out.println("\t" + node.getNodeName() + " ->" + node.getNodeValue());
        }
        NodeList nl = doc.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            printTree(node);
        }
    } catch (Throwable e) {
        System.out.println("Cannot print!! " + e.getMessage());
    }
}

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   www . ja va2  s. com*/
    }
    return s += " )";
}

From source file:Main.java

/**
 * Get a list of the attribute names of an element.
 * @param e Element to use//from ww w  .j a  v  a  2 s. co  m
 * @return a list with all the names of the attributes of the node
 */
public static List<String> getAttributeNames(final Element e) {

    if (e == null) {
        return null;
    }

    final List<String> result = new ArrayList<>();

    final NamedNodeMap map = e.getAttributes();

    for (int i = 0; i < map.getLength(); i++) {

        final Node attribute = map.item(i);

        result.add(attribute.getNodeName());
    }

    return result;
}