码迷,mamicode.com
首页 > 编程语言 > 详细

Java读写xml文件的一些经验(使用dom4j)

时间:2014-11-01 13:27:37      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   os   ar   使用   

说来惭愧,给很多人说过怎么用Java读写xml,但是自己上手做的很少。这篇博客里面简单总结一下。

据网上很多文章介绍,dom4j是一种常用的xml读写API。不过用的时候首先要注意第一个问题:如果在Bing.com里搜索dom4j,排在第一名的是:http://www.dom4j.org/ 很遗憾,这个域名和dom4j没有任何关系,其在Sourceforge上的主页是:http://sourceforge.net/projects/dom4j/ ,我这里下载的是稳定版,将dom4j-1.6.1.jar加入项目的Java Build Path中,我们就可以使用dom4j了。

由于网上dom4j的教程很多,我这里只贴上我自己的代码:

public static void ReadandWritexml(String testCaseName){
        XMLWriter writer = null;
        SAXReader reader = new SAXReader();
        OutputFormat format = OutputFormat.createPrettyPrint();
 
        String filePath = "build.xml";
        File file = new File(filePath);
        try {
            Document document=reader.read(file);
            Element root=document.getRootElement();
            for(Iterator i=root.elementIterator("target");i.hasNext();){
                Element target = (Element) i.next();
                if(target.attributeValue("name").equals("test2")){
                    System.out.println(target);
                    Iterator j=target.elementIterator("junit");
                    Element junit = (Element) j.next();
                    Iterator k=junit.elementIterator("test");
                    Element test = (Element) k.next();
                    test.setAttributeValue("name", testCaseName);
                }
            }
            try {
                writer=new XMLWriter(new FileWriter(filePath),format);
                writer.write(document);
                writer.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

对应的build.xml文件如下:

<target name="test2" depends="compile-main">
  <mkdir dir="test-results-2"/>
  <junit printsummary="on" errorProperty="test.failed" failureProperty="test.failed" fork="true" showoutput="true" haltonfailure="yes">
    <formatter type="plain"/>
    <formatter type="xml"/>
    <jvmarg value="-XX:-UseSplitVerifier"/>
    <classpath refid="test.classpath"/>
    <syspropertyset refid="junit.properties"/>
    <jvmarg value="${poi.test.locale}"/>
    <jvmarg value="-ea"/>
    <jvmarg value="-Xms512m"/>
    <jvmarg value="-Xmx512m"/>
    <jvmarg value="-XX:-UseGCOverheadLimit"/>
    <jvmarg value="-XX:+UseConcMarkSweepGC"/>
    <jvmarg value="-javaagent:lib/aspectjweaver-1.6.11.jar"/>
    <test todir="test-results-2" name="org.apache.poi.ddf.TestEscherBlipRecord"/>
  </junit>
  <junitreport>
    <fileset dir="test-results-2" includes="*.xml"/>
    <report todir="test-results-2"/>
  </junitreport>
  <fail message="Tests failed!" if="test.failed"/>
</target>

Java读写xml文件的一些经验(使用dom4j)

标签:des   style   blog   http   io   color   os   ar   使用   

原文地址:http://blog.csdn.net/qysh123/article/details/40679863

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!