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

/**
 * Build {@link Map} of namespace URIs to prefixes.
 * /* w w  w . j ava  2 s . c  om*/
 * @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

/**
 * @param element/*from ww w. j  av  a2  s .com*/
 * @param map
 * @return
 */
private static boolean fillAttsIntoMap(Element element, Map<String, String> map) {
    /*
     * this case is applicable only if there are no child elements
     */
    if (element.getChildNodes().getLength() > 0) {
        return false;
    }

    /*
     * in case this is not a special case, it will not have attributes, and
     * hence we are safe
     */
    NamedNodeMap attrs = element.getAttributes();
    int n = attrs.getLength();
    for (int i = 0; i < n; i++) {
        Node att = attrs.item(i);
        map.put(att.getNodeName(), att.getNodeValue());
    }
    return true;
}

From source file:Main.java

public static <T> T loadBean(Node node, Class<T> beanClass)
        throws IntrospectionException, InstantiationException, IllegalAccessException, IllegalArgumentException,
        DOMException, InvocationTargetException {
    T store = beanClass.newInstance();//from w w  w .  j  a va 2  s .c  om

    Map<String, PropertyDescriptor> properties = new HashMap<String, PropertyDescriptor>();
    BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
    for (PropertyDescriptor property : beanInfo.getPropertyDescriptors()) {
        properties.put(property.getName(), property);
    }

    NamedNodeMap attributes = node.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Node attribute = attributes.item(i);
        PropertyDescriptor property = properties.get(attribute.getNodeName());
        if (property != null && property.getPropertyType().equals(String.class)
                && property.getWriteMethod() != null) {
            property.getWriteMethod().invoke(store, attribute.getNodeValue());
        }
    }

    NodeList childNodes = node.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        PropertyDescriptor property = properties.get(child.getNodeName());
        if (property != null && property.getPropertyType().equals(String.class)
                && property.getWriteMethod() != null) {
            property.getWriteMethod().invoke(store, child.getTextContent());
        }
    }

    return store;
}

From source file:Main.java

/**
 * Get all attributes as a Map from a node
 *
 * @param node the node taht contains the attributes to read
 * @return the values or a empty map//from  w  w w  .j a  v  a  2 s  .co m
 */
public static HashMap<String, String> getAttributes(Node node) {
    final HashMap<String, String> result = new HashMap<>();

    final NamedNodeMap atts = node.getAttributes();
    for (int i = 0; i < atts.getLength(); i++) {
        final Node att = atts.item(i);
        result.put(att.getNodeName(), att.getNodeValue());
    }
    return result;
}

From source file:Main.java

public static List<Node> getChildren(Node node) {
    ArrayList<Node> children = new ArrayList<Node>();
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        children.add(nodeList.item(i));//w w  w .  ja  v a2 s .c  o  m
    }
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            children.add(attributes.item(i));
        }
    }
    return children;
}

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 .  java  2  s.  co  m*/

    return map;
}

From source file:TryDOM.java

static void listNodes(Node node, String indent) {
        String nodeName = node.getNodeName();
        System.out.println(indent + nodeName + " Node, type is " + node.getClass().getName() + ":");
        System.out.println(node);

        if (node instanceof Element && node.hasAttributes()) {
            NamedNodeMap attrs = node.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Attr attribute = (Attr) attrs.item(i);
                System.out.println(indent + attribute.getName() + "=" + attribute.getValue());
            }//from   w  w w  .  j ava  2  s  .  c om
        }
        NodeList list = node.getChildNodes();
        if (list.getLength() > 0) {
            System.out.println(indent + "Child Nodes of " + nodeName + " are:");
            for (int i = 0; i < list.getLength(); i++)
                listNodes(list.item(i), indent + " ");
        }
    }

From source file:MainClass.java

static void listNodes(Node node) {
    String nodeName = node.getNodeName();

    if (node instanceof Element) {
        if (node.hasAttributes()) {
            NamedNodeMap attrs = node.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Attr attribute = (Attr) attrs.item(i); // Get an attribute
                System.out.println(" " + attribute.getName() + "=" + attribute.getValue());
            }//from  ww w  .j av a2s.  c  o  m
        }
        System.out.println(indent + "<" + nodeName + ">");
    } else if (node instanceof Text) {
        System.out.println(((Text) node).getData());
    } else if (node instanceof DocumentType) {
        System.out.println(getDoctypeString((DocumentType) node));
    }

    indent.append(' ');
    NodeList list = node.getChildNodes();
    if (list.getLength() > 0) {
        for (int i = 0; i < list.getLength(); i++) {
            listNodes(list.item(i));
        }
    }
    System.out.println("</" + nodeName + ">");

}

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.ja v a 2  s. co  m*/
                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

private static void attributize(Element root) {
    NamedNodeMap attributeMap = root.getAttributes();
    for (int i = 0; i < attributeMap.getLength(); i++) {
        org.w3c.dom.Attr attr = (Attr) attributeMap.item(i);

        Element attrElement = root.getOwnerDocument().createElement(attr.getName());
        attrElement.setTextContent(attr.getValue());
        root.appendChild(attrElement);// w  w w  . ja  v  a  2s  .c o  m
    }

    NodeList children = root.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i) instanceof Element) {
            attributize((Element) children.item(i));
        }
    }
}