通过递归实现java xml转json

来源:互联网 发布:mac 输入法 emoji表情 编辑:程序博客网 时间:2024/04/30 03:44
  /**

     * xml转换成json

     * @param node
     * @return
     */
    public static JSONObject nodeToJson(org.dom4j.Element node){
List<org.dom4j.Element> els = node.elements();
int size = els.size();
if(size>0){
JSONObject map = new JSONObject();
for(int j=0;j<size;j++){
org.dom4j.Element childNode = els.get(j);
JSONObject m = nodeToJson(childNode);
if(m==null){
map.put(childNode.getName(), childNode.getText());
}else{
map.put(childNode.getName(), m);
}
}
return map ;
}else{
return null;
}

}


public void static main(String[] args){

nodeToJson(org.dom4j.DocumentHelper.parseText(result).getRootElement())

}

0 0