使用dom4j.jar操作XML

来源:互联网 发布:什么软件做假章 编辑:程序博客网 时间:2024/05/28 16:22

dom4j.jar是用来操作xml的java工具包

1.下面做dom4j.jar解析xml操作
2.junit如果不了解,请移步java junit测试
3.jar包与原代码将在以后一起提供

所需要的jar

public class TestXML {    @SuppressWarnings("unused")    @Test    public void f1(){        SAXReader reader=new SAXReader();        Document document=null;        try {            document= reader.read(TestXML.class.getResourceAsStream("/spring.xml"));        } catch (DocumentException e) {            e.printStackTrace();            throw new RuntimeException("给定的路径,找不到配置文件");        }        String xPath="//bean";        List<Element> list= document.selectNodes(xPath);        System.out.println(list.toString());        System.out.println("ok");    }    @Test    public void f2(){        System.out.println(TestXML.class.getResourceAsStream("/spring.xml"));    }}
spring.xml
<?xml version="1.0" encoding="UTF-8"?><beans>    <bean name="a" class="zx.mes.hyl.bean.A">        <property name="name" value="张三"></property>        <property name="age" value="33"></property>    </bean>    <bean name="b" class="zx.mes.hyl.bean.B">        <property name="name" value="李四"></property>        <property name="a" ref="a"></property>    </bean></beans>