Some experience with Java reading and writing XML files (using dom4j)

Source: Internet
Author: User

It's a shame to say to many people how to read and write XML in Java, but they do very little. A brief summary of this blog post.

According to many articles on the internet, DOM4J is a common XML read-write API. But when using the first thing to pay attention to the first question: If in bing.com search dom4j, ranked in the first place is: http://www.dom4j.org/Unfortunately, this domain name and dom4j do not have any relationship, its homepage on the SourceForge is: http ://sourceforge.net/projects/dom4j/, I'm downloading the stable version here, adding Dom4j-1.6.1.jar to the Java Build path of the project, we can use the dom4j.

Due to the many tutorials on the Web dom4j, I only post my own code here:

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 (); }    }

the corresponding build.xml files are as follows:

<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"/> <c Lasspath refid= "Test.classpath"/> <syspropertyset refid= "junit.properties"/> <jvmarg value= "${poi.test. Locale} "/> <jvmarg value="-ea "/> <jvmarg value="-xms512m "/> <jvmarg value="-xmx512m "/> &L T;jvmarg value= "-xx:-usegcoverheadlimit"/> <jvmarg value= "-xx:+useconcmarksweepgc"/> <jvmarg value= "-ja Vaagent: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> 

Some experience with Java reading and writing XML files (using dom4j)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.