JAVA 解析XML之JDOM、DOM4J方式

JDOM、DOM4J解析XML方式是在DOM的基础上完成的,都要导入特定jar包来获取API接口,是通过节点的层级关系来获取节点内容的。

1、JDOM 方式

导入jdom-2.0.5.jar

/**
 * 
 * 用JDOM的方式解析XML文件
 * 
 * */

public class JDOM_Parse {

    public static void main(String[] args) {
        //1、获取SAXBuilder对象
        SAXBuilder builder=new SAXBuilder();
        //2、创建一个输入流,加载xml文件
        InputStream is;
        try {
            is=new FileInputStream("department.xml");
            //3、创建Document对象
            Document document=builder.build(is);
            //4、获取根节点
            Element rootElement=document.detachRootElement();
            //5、获取所有子节点
            List<Element> childElement=rootElement.getChildren();
            for (Element element : childElement) {
                //获取当前子节点的所有属性
                List<Attribute> attributes=element.getAttributes();
                for (Attribute attr : attributes) {
                    System.out.print(attr.getName()+": " +attr.getValue()+",");
                }
                System.out.println();
                List<Element> childNode = element.getChildren();
                for (Element node : childNode) {
                    System.out.println("节点名:"+node.getName()+"  节点值:"+node.getValue());
                }
                System.out.println();
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JDOMException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

2、DOM4J方式

导入dom4j-1.6.1.jar

public class DOM4J_parse {

    public static void main(String[] args) {
        SAXReader reader=new SAXReader();
        try {
            Document document = reader.read(new File("department.xml"));
            Element rootElement = document.getRootElement();
            Iterator it= rootElement.elementIterator();
            while (it.hasNext()) {
                Element element = (Element) it.next();
                List<Attribute> attributes=element.attributes();
                System.out.println();
                for (Attribute attr : attributes) {
                    System.out.print("属性名:"+attr.getName()+" ==== 属性值:"+attr.getValue()+" , ");
                }
                Iterator childIt = element.elementIterator();
                while (childIt.hasNext()) {
                    Element node = (Element) childIt.next();
                    System.out.println("节点名: "+node.getName()+" ----- 节点值: "+node.getText());
                }
                System.out.println();
            }



        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值