Java tutorial
//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(); 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; } }