Believe that as a developer you should be very familiar with XML and JSON. About both the introduction and the difference on the internet has a lot of information, Java for XML parsing also has a lot of toolkit, the main parsing methods have three kinds:
1.DOM parsing: For parsing small XML files, if you manipulate large XML files, reading the entire docment into memory may cause low memory.
2.SAX parsing method: The memory consumption is small, suitable for parsing large XML files, the white is scanning parsing.
3.DIGESTER/JAXB: Struts's XML parsing tool digester.
This article mainly introduces the use of DOM4J, DOM4J has integrated the SAX mode. You can use DOM4J to handle the operation of large XML files. For the digester author does not have too much contact, here do not do too much explanation.
Let's look at the use of dom4j.
I. Login official website http://www.dom4j.org/
If you can't find the download location, go to github download (http://dom4j.github.io/)
Two. Unzip the downloaded compressed package. You will see the (Dom4j-1.6.1.jar) package, and the jar package will be imported into the project.
Three. Simple read and write operation, the following is the operation code:
public class xmltest { string filename = "d:/ Test/test.xml "; @Test public void testwritexml () { try { OutputFormat format = Outputformat.createprettyprint ();//Create a formatted class format.setencoding ("UTF-8");//Set Code format.setindent (TRUE);//whether indent format.setnewlines (TRUE);//whether line wrapping xmlwriter xmlwriter = new xmlwriter (new FileWriter (FileName), format) //If you need to set the file encoding format, You need to create a writer (1) //xmlwriter = new xmlwriter (New outputstreamwriter (New FileOutputStream ( New file (fileName)), "UTF-8"), format); document document = documenthelper.createdocument ();// Create document from the Documenthelper tool class element root = document.addelement ("root"); root.addelement ("item"). AddAttribute ("type", "T"). AddText ("text "); xmlwriter.write ( Document); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; xmlwriter.close ();//must be closed (2) } catch (ioexception e) { e.printstacktrace (); } } // @Test public void testreadxml () {//XML file read-once saxreader reader = new saxreader (); try { document document = reader.read (New filereader (fileName)); Element root = Document.getrootelement (); &nbsP; for (Iterator<?> i = root.elementiterator (); i.hasnext ();) {//Traversal node Element ele = (Element) i.next (); System.out.println (Ele.asxml ()); } } catch (FileNotFoundException e) { E.printstacktrace (); } catch ( Documentexception e) { e.printstacktrace (); } } @Test public void &NBSP;TESTWRITEXML1 () {//write large XML file, the following method is recommended try { OutputFormat Format = outputformat.createprettyprint (); format.setencoding ("UTF-8");//Set Code format.setindent (TRUE);//whether indent format.setnewlines (TRUE);//whether line wrapping xmlwriter xmlwriter = new xmlwriter ( New filewriter (FileName), Format); document Document = documenthelper.createdocument ();//Create document by Documenthelper tool class xmlwriter.write (document); element root = document.addelement ( "Root"); element item = documenthelper.createelement ("item"); xmlwriter.writeopen (root);//Prepare to write for (int i=0;i<1000000;i++) { item.addattribute ("type", "type" +i). AddText ("text" +i); Item.setparent (Root); xmlwriter.write (item);//write Node item.clearcontent ();//Emptying node properties } xmlwriter.writeclose (root);//write Complete xmlwriter.close ();//must be closed } catch (ioexception e) { e.printstacktrace (); } } @Test public void testsaxreader () {//scan to read XML file, need to implement elementpath interface, logic processing in this interface saxreader reader = new saxreader (); reader.setdefaulthandler (New elementhandler () { @Override public void onstart (ElementPath ElementPath) { element element = elementpath.getcurrent (); if ("Item". Equals (Element.getName ())) { System.out.println (Element.attributevalue ("type")); system.out.println ( Element.gettext ());//not get text value (3) } } @Override public void onend (ElementPath ElementPath) { element element = elementpaTh.getcurrent (); if ("Item". Equals (Element.getname ())) { system.out.println ( Element.attributevalue ("type")); system.out.println (Element.gettext ()); } } }); try { reader.read (New FileReader (fileName)); } catch (filenotfoundexception e) { e.printstacktrace (); } catch (documentexception e) { e.printstacktrace (); } } }
Four: Precautions:
(1): Generate XML file encoding, if there is a Chinese need to be aware of the configuration here.
(2): Always remember to close write.
(3): Elementhandler interface, need to implement onstart (initialization operation), OnEnd method (processing logic), note here in the OnStart method will not be able to get the text value of the node (may not have read the operation).
Note: If you need more detailed information, please refer to the official documentation
Reference: http://www.ibm.com/developerworks/cn/xml/dm-1208gub/
Use of dom4j and FAQs