Parse small demo of XML file with JDOM

Source: Internet
Author: User

JDOM is a unique Java toolkit that uses XML to quickly develop XMLApplicationProgram. Its design includes the syntax and even semantics of the Java language.

Click to download the JDOM jar package

I made a Java project example to introduce the file structure first:


My XML document is placed under the src directory. The specific content is as follows:

 

 
<? XML version = "1.0" encoding = "UTF-8"?> <Root> <person id = "1"> <username> ydcun </username> <password> 123123 </password> </person> <person id = "2"> <username> 1111111112 </username> <password> password2 </password> </person> </root>

The following describes how to use JDOM to operate XML files. I only wrote examples of traversing, deleting, and adding nodes:

 

 

Package COM. ydcun. test; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. inputstream; import Java. util. list; import Org. JDOM. document; import Org. JDOM. element; import Org. JDOM. jdomexception; import Org. JDOM. input. saxbuilder; import Org. JDOM. output. xmloutputter; public class testdemo {Private Static document; // stores the Read File Private Static string Path = "src/struts. XML "; // file storage path // Load file public testdemo () throws exception {saxbuilder bulider = new saxbuilder (); inputstream inst = new fileinputstream (PATH); document = bulider. build (insT);} // traverse the parsing document public void xmlparse () {element root = document. getrootelement (); // obtain the Root Node object list <element> List = root. getchildren (); For (element El: List) {system. out. println ("ID =" + el. getattributevalue ("ID"); system. out. println ("name:" + el. getchildtext ("username"); system. out. println ("Password:" + el. getchildtext ("password") ;}// Add the public void addelement () throws exception {// create a person node element El = new element ("person "); el. setattribute ("ID", "3"); // create the username node element elname = new element ("username"); elname. settext ("Qiqi"); // create the password node element elpassword = new element ("password"); elpassword. settext ("123456"); // Add username and password to El. addcontent (elname); El. addcontent (elpassword); // get the root node to add the person node to the root node element root = document. getrootelement (); root. addcontent (EL); document. setrootelement (Root); // Save the added content to the file xmloutputter out = new xmloutputter (); out. output (document, new fileoutputstream (PATH);} // Delete the public void deleteelement (int id) throws exception {element root = document. getrootelement (); List <element> List = root. getchildren (); For (element El: List) {If (El. getattributevalue ("ID "). equals (ID + "") {root. removecontent (EL); // Delete the qualified node} xmloutputter out = new xmloutputter (); out. output (document, new fileoutputstream (PATH);} public static void main (string [] ARGs) throws exception {testdemo TD = new testdemo (); // TD. addelement (); // Add a node // TD. xmlparse (); // convenience XML file TD. deleteelement (3); // delete node }}

Through the above operations, we can easily master the XML file operations!

 








 

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.