Dom4解析XML数据示例

menu.xml

<?xml version="1.0" encoding="UTF-8"?>
<menus>
	<menu id="001">
		<name>权限管理</name>
		<children>
			<menu id="001001">
				<name>用户管理</name>
				<children></children>
			</menu>
			<menu id="001002">
				<name>角色管理</name>
				<children></children>
			</menu>
		</children>
	</menu>
	<menu id="002">
		<name>系统管理</name>
		<children></children>
	</menu>
</menus>

 

 

解析

 

package com.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.model.TreeNode;

/**
 * 业务方法.
 * @author 尔演&Eryan [email protected]
 * @date 2013-7-15 下午10:24:41 
 * @version 1.0
 */
public class XMLService {

	public static void main(String[] args) {
		new XMLService().readMenuXml();
	}
	
	/**
	 * 读取文件
	 * @return
	 */
	private Document readXML() {  
        Document document = null;  
        SAXReader saxReader = new SAXReader();  
        try {  
            document = saxReader.read(getClass().getClassLoader().getResourceAsStream("menu.xml"));  
        } catch (DocumentException e) {  
            e.printStackTrace();  
        }  
        return document;  
    }  
	
	public void readMenuXml(){
		Document doc = readXML();
		//根元素
		Element root = doc.getRootElement();
		
		List<?> menus = root.elements("menu");
		if (menus.size() == 0) { 
            //没有子元素 
        } else { 
            //有子元素 
            for (Iterator it = menus.iterator(); it.hasNext();) { 
                Element elem = (Element) it.next(); 
                System.out.println("id:"+elem.attributeValue("id")+" name:"+elem.elementText("name"));
                //递归
                getElementList(elem.element("children"));
            } 
        } 
	}
	
	
	/** 
     * 递归遍历方法 
     * 
     * @param element 
     */ 
    public void getElementList(Element element) { 
        List elements = element.elements("menu"); 
        if (elements.size() == 0) { 
            //没有子元素 
        } else { 
            //有子元素 
            for (Iterator it = elements.iterator(); it.hasNext();) { 
                Element elem = (Element) it.next(); 
                System.out.println("id:"+elem.attributeValue("id")+" name:"+elem.elementText("name"));
                //递归遍历 
                getElementList(elem.element("children")); 
            } 
        } 
    } 
	
    /**
     * 模拟数据
     * <br>此处未使用递归
     * @return
     */
    public List<TreeNode> getMenusByTree(){
    	
    	List<TreeNode> rootNodes = new ArrayList<TreeNode>();
    	TreeNode rootNde1 = new TreeNode("001","权限管理");
    	TreeNode rootNde2 = new TreeNode("002","系统管理");
    	
    	List<TreeNode> childs1 = new ArrayList<TreeNode>();
    	TreeNode children1 = new TreeNode("001001", "用户管理");
    	TreeNode children2 = new TreeNode("001002", "角色管理");
    	childs1.add(children1);
    	childs1.add(children2);
    	
    	//设置子节点
    	rootNde1.setChildren(childs1);
    	
    	rootNodes.add(rootNde1);
    	rootNodes.add(rootNde2);
    	
		return rootNodes;
    }
	
}

 输出结果

id:001 name:权限管理
id:001001 name:用户管理
id:001002 name:角色管理
id:002 name:系统管理

 

你可能感兴趣的:(dom4j)