递归读取xml 文件;

package cn;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Attr;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class CopyOfCopyOfTest {

    private List<Page> pageList = new ArrayList<Page>();

    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();

        Document doc = db.parse(new File("d:\\catelog.xml"));
        // 获取根元素节点
        Element root = doc.getDocumentElement();

        // 这种读取方式直接读取整个节点; 相当于直接把所有的都读取了; 不是像pullparser 一样有个指针一样的东西;
        List<CatelogNode> childrennew = parseElement(root, null, null);

    }

    static List<CatelogNode> parseElement(Element element, CatelogNode parent,
            List<CatelogNode> catelogNodeList) {

        String tagName = element.getNodeName();

        // System.out.print("<" + tagName);

        List<Page> pageList = new ArrayList<Page>();

        if ("Pages".equalsIgnoreCase(tagName)) {
            // Page 对象集合;
            NodeList children = element.getChildNodes();

            Integer length = children.getLength();
            for (int index = 0; index < length; index++) {
                // 获取每一个child Page 对象;
                Node pageNode = children.item(index);

                // 获取节点类型
                short nodeType = pageNode.getNodeType();

                if (nodeType == Node.ELEMENT_NODE) {
                    // 这里需要在这里进行 new 否则 可能出现不匹配的情况;
                    Page page = new Page();
                    pageList.add(page);
                    NamedNodeMap map = pageNode.getAttributes();
                    // 如果存在属性,则打印属性
                    if (null != map) {
                        for (int i = 0; i < map.getLength(); i++) {
                            // 获得该元素的每一个属性
                            Attr attr = (Attr) map.item(i);
                            // 属性名和属性值
                            String attrName = attr.getName();
                            String attrValue = attr.getValue();
                            // 注意属性值需要加上引号,所以需要\转义

                            if ("name".equals(attrName)) {
                                page.setName(attrValue);

                            }

                            NodeList rootNodeList = pageNode.getChildNodes();
                            for (int rootIndex = 0; rootIndex < rootNodeList
                                    .getLength(); rootIndex++) {

                                {

                                    Node rootNode = rootNodeList
                                            .item(rootIndex);

                                    // 获取节点类型
                                    short rootNodeType = rootNode.getNodeType();

                                    if (rootNodeType == Node.ELEMENT_NODE) {

                                        NamedNodeMap rootMap = rootNode
                                                .getAttributes();

                                        Root root = new Root();

                                        page.setRoot(root);

                                        // 如果存在属性,则打印属性
                                        if (null != rootMap) {
                                            for (int rootAttrIndex = 0; rootAttrIndex < rootMap
                                                    .getLength(); rootAttrIndex++) {

                                                Attr rootAttr = (Attr) rootMap
                                                        .item(rootAttrIndex);
                                                // // 属性名和属性值
                                                String rootAttrName = rootAttr
                                                        .getName();
                                                String rootAttrValue = rootAttr
                                                        .getValue();

                                                if ("name".equals(rootAttrName)) {

                                                    root.setName(rootAttrValue);

                                                } else if ("tag"
                                                        .equals(rootAttrName)) {

                                                    root.setTag(rootAttrValue);

                                                } else if ("bkImg"
                                                        .equals(rootAttrName)) {

                                                    root
                                                            .setBkImg(rootAttrValue);

                                                }

                                            }
                                        }

                                        // 便利直接的子节点;
                                        NodeList menuNodeList = rootNode
                                                .getChildNodes();
                                        List<CatelogNode> directChildrenList = new ArrayList<CatelogNode>();
                                        for (int menuIndex = 0; menuIndex < menuNodeList
                                                .getLength(); menuIndex++) {

                                            {

                                                // ---------------------
                                                try {
                                                    Node menuNode = menuNodeList
                                                            .item(menuIndex);

                                                    // 获取节点类型
                                                    short menuNodeType = menuNode
                                                            .getNodeType();
                                                    if (menuNodeType == menuNode.ELEMENT_NODE) {

                                                        CatelogNode

                                                        node = toCatelogNode(menuNode);

                                                        System.out
                                                                .println("6----"
                                                                        + node
                                                                                .getName()+"bk:"+node.getBkImg());
                                                        
                                                        // 设置root 的 直接的catelogList
                                                        directChildrenList
                                                                .add(node);
                                                        
                                                        List<CatelogNode> childrenList = new ArrayList<CatelogNode>();
                                                        //CatelogNode parentCatelogNode = toCatelogNode(menuNode);
                                                        // 递归所有的 直接的menu  形成list 返回;
                                                        List<CatelogNode> mynewList = toTree(
                                                                node,
                                                                childrenList,
                                                                menuNode);
                                                        node.setChildren(mynewList);
                                                        
                                                        
                                                        
                                                        
                                                        
                                                        
                                                        
                                                        

                                                    }
                                                } catch (Exception e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        }

                                        root
                                                .setCatelogNodeList(directChildrenList);

                                        for (int menuIndex = 0; menuIndex < menuNodeList
                                                .getLength(); menuIndex++) {

                                            {

                                                // ---------------------

                                                Node menuNode = rootNodeList
                                                        .item(rootIndex);

                                                // 获取节点类型
                                                short menuNodeType = menuNode
                                                        .getNodeType();
                                                try {
                                                    if (menuNodeType == menuNode.ELEMENT_NODE) {

//                                                        List<CatelogNode> childrenList = new ArrayList<CatelogNode>();
//                                                        CatelogNode parentCatelogNode = toCatelogNode(menuNode);
//                                                        // 递归所有的 直接的menu  形成list 返回;
//                                                        List<CatelogNode> mynewList = toTree(
//                                                                parentCatelogNode,
//                                                                childrenList,
//                                                                menuNode);

//                                                        for (int ik = 0; ik < mynewList
//                                                                .size(); ik++) {

                                                            // System.out
                                                            // .println(mynewList
                                                            // .get(ik)
                                                            //
                                                            // );
//                                                        }
                                                    }

                                                } catch (Exception e) {
                                                    e.printStackTrace();
                                                }

                                            }
                                        }

                                        // System.out.println(root);
                                    }

                                    // attr end;

                                }

                            }
                        }
                    }
                }
            }

            System.out.println("pageSize-------------" + pageList.size());

//            for (int i = 0; i < pageList.size(); i++) {
//
//                System.out
//                        .println("firstsize:---------------------"
//                                + i
//                                + "---------------"
//                                + pageList.get(i).getRoot()
//                                        .getCatelogNodeList().size());
                //
                // System.out.println("se:" + i + "::::::::"
                // + pageList.get(i).getRoot());
//            }

            
            List <CatelogNode> mylist=
                pageList.get(0).getRoot().getCatelogNodeList();
            
            for(int i=0;i<mylist.size();i++){
                
                System.out.println(mylist.get(i));
                
                
                
                
                
            }
        
        }
        return catelogNodeList;
    }

    private static CatelogNode toCatelogNode(Node menuNode) throws Exception {
        short nodeType = menuNode.getNodeType();
        CatelogNode currentNode = new CatelogNode();

        if (nodeType == Node.ELEMENT_NODE) {

            NamedNodeMap map = menuNode.getAttributes();
            // 如果存在属性,则打印属性
            if (null != map) {
                for (int i = 0; i < map.getLength(); i++) {
                    // 获得该元素的每一个属性
                    Attr attr = (Attr) map.item(i);
                    // 属性名和属性值
                    String attrName = attr.getName();
                    String attrValue = attr.getValue();
                    // 注意属性值需要加上引号,所以需要\转义
                    // System.out.print(" " + attrName + "=\"" + attrValue +
                    // "\"");

                    if (currentNode != null) {// isMenu &&
                        if ("name".equals(attrName)) {
                            currentNode.setName(attrValue);
                        } else if ("tag".equals(attrName)) {
                            currentNode.setTag(attrValue);
                        } else if ("packFile".equals(attrName)) {
                            currentNode.setPackFile(attrValue);
                        } else if ("bkImg".equals(attrName)) {
                            currentNode.setBkImg(attrValue);
                        } else if ("btnPos".equals(attrName)) {
                            currentNode.setBtnPos(attrValue);
                        } else if ("btnSize".equals(attrName)) {
                            currentNode.setBtnSize(attrValue);
                        } else if ("rtnBtnPos".equals(attrName)) {
                            currentNode.setRtnBtnPos(attrValue);
                        } else if ("rtnBtnSize".equals(attrName)) {
                            currentNode.setRtnBtnSize(attrValue);
                        } else if ("homeBtnPos".equals(attrName)) {
                            currentNode.setHomeBtnPos(attrValue);
                        } else if ("homeBtnSize".equals(attrName)) {
                            currentNode.setHomeBtnSize(attrValue);
                        } else if ("type".equals(attrName)) {
                            currentNode.setType(attrValue);
                        } else if ("attr".equals(attrName)) {
                            currentNode.setAttr(attrValue);
                        }
                    }
                }
            }
        }
        return currentNode;

    }

    private static List<CatelogNode> toTree(CatelogNode parent,
            List<CatelogNode> tempList, Node parentNode) throws Exception {

        NodeList childrenList = parentNode.getChildNodes();

        if (childrenList != null && childrenList.getLength() > 0) {

            for (int i = 0; i < childrenList.getLength(); i++) {

                Node node = childrenList.item(i);

                // 获取节点类型
                short nodeType = node.getNodeType();

                if (nodeType == Node.ELEMENT_NODE) {

                    CatelogNode chidl = toCatelogNode(node);

                    List<CatelogNode> sonList = new ArrayList<CatelogNode>();

                    chidl.setChildren(sonList);
                    tempList.add(chidl);
                    toTree(chidl, sonList, node);
                }
            }

        }

        return tempList;

    }
}

猜你喜欢

转载自blog.csdn.net/u010509143/article/details/38533397