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 Map<String, String> findAttributesValues(Node node) {

    Map<String, String> retval = new HashMap<String, String>();
    NamedNodeMap attrs = node.getAttributes();
    if (attrs != null) {
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attr = attrs.item(i);
            String name = attr.getNodeName();
            String value = attr.getNodeValue();

            retval.put(name, value);/*  w  w w. j  ava 2  s. com*/
        }
    }

    return retval;
}

From source file:Main.java

/**
 * Puts all Nodes of the source NamedNodeMap into the destination NamedNodeMap
 * /*from w  w  w . ja  v a 2 s .c o m*/
 * @param dst the destination NamedNodeMap
 * @param src the source NamedNodeMap
 **/
public final static void putAll(final NamedNodeMap dst, final NamedNodeMap src) {
    final int size = size(src);

    for (int i = 0; i < size; i++) {
        dst.setNamedItemNS(src.item(i));
    }
}

From source file:Main.java

public static Attr[] getAttrs(Element elem) {
    NamedNodeMap attrMap = elem.getAttributes();
    Attr[] attrArray = new Attr[attrMap.getLength()];
    for (int i = 0; i < attrMap.getLength(); i++)
        attrArray[i] = (Attr) attrMap.item(i);
    return attrArray;
}

From source file:Main.java

public static List<String> getAllAttrValueByName(Node item, String name) {
    List<String> lst = null;
    NamedNodeMap attributes = item.getAttributes();
    int numAttrs = attributes.getLength();
    for (int i = 0; i < numAttrs; i++) {
        Attr attr = (Attr) attributes.item(i);
        String attrName = attr.getNodeName();
        if (name.equals(attrName)) {
            if (lst == null) {
                lst = new ArrayList();
            }//from  www . j a v a  2s  .  c  o  m
            lst.add(attr.getNodeValue());
        }
    }
    return lst;
}

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  a  2  s . c  om
        }

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

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

    return null;
}

From source file:Main.java

/**
 * Convert an XML fragment from one namespace to another.
 *
 * @param from      element to translate
 * @param namespace namespace to be translated to
 * @return the element in the new namespace
 *
 * @since 8.4/*  ww  w .  jav a  2  s. co  m*/
 */
public static Element translateXML(Element from, String namespace) {
    Element to = from.getOwnerDocument().createElementNS(namespace, from.getLocalName());
    NodeList nl = from.getChildNodes();
    int length = nl.getLength();
    for (int i = 0; i < length; i++) {
        Node node = nl.item(i);
        Node newNode;
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            newNode = translateXML((Element) node, namespace);
        } else {
            newNode = node.cloneNode(true);
        }
        to.appendChild(newNode);
    }
    NamedNodeMap m = from.getAttributes();
    for (int i = 0; i < m.getLength(); i++) {
        Node attr = m.item(i);
        to.setAttribute(attr.getNodeName(), attr.getNodeValue());
    }
    return to;
}

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   ww w. ja v a  2s . 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

/**
 * // w w  w .  jav  a  2 s.  co  m
 * @param node
 * @param classname
 * @return
 */
public static Object decode(Element node, String classname) {
    try {
        Class classObject = Class.forName(classname);
        Object object = classObject.newInstance();
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node child = attributes.item(i);
            String nodeName = child.getNodeName();
            Field field = classObject.getField(nodeName);
            field.setAccessible(true);
            field.set(object, child.getNodeValue());
        }
        return object;
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        return null;
    } catch (InstantiationException e) {
        e.printStackTrace();
        return null;
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        return null;
    } catch (SecurityException e) {
        e.printStackTrace();
        return null;
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:Main.java

public static String getAttrvalue(Node item, String name, boolean ignoreNs) {
    NamedNodeMap attributes = item.getAttributes();
    int numAttrs = attributes.getLength();
    for (int i = 0; i < numAttrs; i++) {
        Attr attr = (Attr) attributes.item(i);
        String attrName = attr.getNodeName();
        String NSName = attr.getNamespaceURI();
        if (ignoreNs) {
            if ((attrName.indexOf(":" + name) != -1) || (name.equals(attrName)))
                return attr.getNodeValue();
        } else {//from   w  w  w  .  j a  va 2  s .c  o m
            if (name.equals(attrName)) {
                return attr.getNodeValue();
            }
        }
    }
    return null;
}

From source file:Main.java

private static String readXsdVersionFromFile(Document doc) {
    final String JBOSS_ESB = "jbossesb";
    NodeList nodes = doc.getChildNodes();
    if (nodes != null) {
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            if (JBOSS_ESB.equals(node.getNodeName())) {
                NamedNodeMap attributes = node.getAttributes();
                for (int j = 0; j < attributes.getLength(); j++) {
                    Node attribute = attributes.item(j);
                    if ("xmlns".equals(attribute.getNodeName())) {
                        String value = attribute.getNodeValue();
                        if (value.contains(JBOSS_ESB) && value.endsWith(".xsd"))
                            return value.substring(value.lastIndexOf('/') + 1, value.length());
                        else
                            throw new IllegalStateException(
                                    "The ESB descriptor points to an invalid XSD" + value);
                    }/*from  w  w w . j  av  a2  s. c om*/
                }
            }
        }
        throw new IllegalArgumentException("No root node " + JBOSS_ESB + " found.");
    } else
        throw new IllegalArgumentException("Descriptor has no root element !");
}