JDOM 方式解析 XML

1.引入jdom jar包

2.

SAXBuilder builder = new SAXBuilder();
        try {
            Document doc = builder.build(new FileInputStream("tree.xml"));
            //获取根节点
            Element root = doc.getRootElement();
            //获取根节点子节点集合
            List<Element> eleList = root.getChildren();
            //遍历子节点
            for (Element ele : eleList) {
                System.out.println("节点名:"+ele.getName());
                //获取单个属性
//                String id =  ele.getAttributeValue("id");
                List<Attribute> attrList = ele.getAttributes();
                for (Attribute attr : attrList) {
                    System.out.println("属性名:"+attr.getName());
                    System.out.println("属性名:"+attr.getValue());
                }
                //获取子节点集合
                List<Element> childList = ele.getChildren();
                //遍历集合
                for (Element child : childList) {
                    System.out.println("节点值:"+child.getValue());
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

 

posted @ 2015-01-19 16:18  知之为知之  阅读(172)  评论(0编辑  收藏  举报