Java XML Element Get from Parent getNodeBean(Node parent)

Here you can find the source of getNodeBean(Node parent)

Description

get Node Bean

License

Open Source License

Declaration

private static Map<?, ?> getNodeBean(Node parent) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.HashMap;

import java.util.List;
import java.util.Map;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    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   w  ww .  j  a v a 2s .  c om*/
                    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;
    }
}

Related

  1. getNamedNodeList(Element parent, String containerTagName)
  2. getNamespacePrefix(Node parentNode, String preferredPrefix, String nsUri)
  3. getNameToFirstNode(Node parent, String node_name)
  4. getNameToNodeList(Node parent, String node_name)
  5. getNodeAtPosition(Node parent, int offset)
  6. getNodeByLocalName(final Node parent, final String name)
  7. getNodeContents(Node parentNode)
  8. getNodeContents(String nodeName, Node parentNode)
  9. getNodeList(Element parent)