There are a lot of ways to manipulate XML files, here's how to use Jdom and tips.
Jdom
Creating an XML document
An XML file is a typical tree file, each of which is a child of a document element. Each child element is an element object, and the object can be contained downward.
1 We can therefore add the top-level element to the root element by first creating the element and then adding the element to the parent element.
2 Once you have created the document element, you can add the element to the documents object and then write to the file.
The main functions used are:
Element.setattribute add information to an element element.addcontent (string,string) adds child element content to the element, or you can add another element node directly document.setrootelement ( Element) adds a root element to the document Xmloutputter.output (Document,filewriter) writes docuemnt to the FileWriter file stream
Here is the main procedure, the process of writing the file into the SaveXML
1@SuppressWarnings ("null")2 Public Static voidCreatexml () {3 //Create document4Document mydoc =NewDocument ();5 6 //Creating an element Person17Element Person1 =NewElement ("Person");8Person1.setattribute ("id", "ID001");9 //Add CommentTenPerson1.addcontent (NewComment ("This is Person1")); One APerson1.addcontent (NewElement ("name"). SetText ("Xingoo"))); -Person1.addcontent (NewElement ("Age"). SetText ("25")); -Person1.addcontent (NewElement ("Sex"). SetText ("M"))); the //can be nested to add child elements -Element Address1 =NewElement ("Address"); -Address1.setattribute ("zone", "Province"); -Address1.addcontent ("Liaoning"); + person1.addcontent (ADDRESS1); - + //Creating an element Person2 AElement Person2 =NewElement ("Person"); atPerson2.setattribute ("id", "ID002"); - //Add Comment -Person2.addcontent (NewComment ("This is Person2")); - -Person2.addcontent (NewElement ("name"). SetText ("Xhalo"))); -Person2.addcontent (NewElement ("Age"). SetText ("26")); inPerson2.addcontent (NewElement ("Sex"). SetText ("M"))); - //can be nested to add child elements toElement Address2 =NewElement ("Address"); +Address2.setattribute ("zone", "Province"); -Address2.addcontent ("Jilin"); the person2.addcontent (address2); * $ //add element in doc personPanax NotoginsengElement info =NewElement ("Information"); - info.addcontent (person1); the info.addcontent (person2); + mydoc.setrootelement (info); A the SaveXML (mydoc); +}
SaveXML () Code:
1 Public Static voidSaveXML (Document doc) {2 //Exporting a Doc object to a file3 Try {4 //creating an XML file output stream5Xmloutputter xmlopt =NewXmloutputter ();6 7 //creating a file output stream8FileWriter writer =NewFileWriter ("Person.xml");9 Ten //Specify document Format OneFormat FM =Format.getprettyformat (); A //fm.setencoding ("GB2312"); - XMLOPT.SETFORMAT (FM); - the //writes doc to the specified file - Xmlopt.output (doc, writer); - writer.close (); -}Catch(Exception e) { + e.printstacktrace (); - } +}
After the execution, refresh the project, you can see the Person.xml file under the project.
Reading an XML document
Read the document, the first need an XML parser, it can automatically parse out the elements, and the elements as their own children node, easy to operate.
The main functions used are:
Saxbuilder.build ("Xxx.xml") parses the XML document Document.getrootelement () gets the root element Element.getchildren () gets the child element under the root element and returns the list <Element>element.getattributevalue (String) Gets the information of the specified element Element.getchildtext gets the contents of the specified element
1 Public Static voidReadXML () {2 //parsing an XML file using the Saxbuilder parser3Saxbuilder SB =NewSaxbuilder ();4Document doc =NULL;5 Try {6doc = Sb.build ("Person.xml");7Element root =doc.getrootelement ();8list<element> list = Root.getchildren ("Person");9 for(Element el:list) {TenString id = el.getattributevalue ("id"); OneString name = El.getchildtext ("name"); AString age = El.getchildtext (' Age '); -String sex = el.getchildtext ("Sex"); -SYSTEM.OUT.PRINTLN ("ID:" +ID); theSystem.out.println ("Name:" +name); -System.out.println ("Age:" +Age ); -System.out.println ("Sex:" +sex); -System.out.println ("--------------------------"); + } -}Catch(Exception e) { + e.printstacktrace (); A } at}
Modifying an XML document
Modifying an XML document is also the first use of the parser to find the specified element, using setText or setattributevalue to modify the element content
Remember to save to the file when you modify it, call SaveXML () here
1 Public Static voidUpdatexml () {2Saxbuilder SB =NewSaxbuilder ();3Document doc =NULL;4 Try {5doc = Sb.build ("Person.xml");6Element root =doc.getrootelement ();7list<element> list = Root.getchildren ("Person");8 for(Element el:list) {9 if(El.getattributevalue ("id"). Equals ("ID001"))) {TenElement name = El.getchild ("name"); OneName.settext ("Xingoo---update"); A } - } -}Catch(Exception e) { the e.printstacktrace (); - } - SaveXML (DOC); -}
Delete an XML document element
The delete operation is like a modification, and calling removecotent can delete the specified element based on the content. But it's going to be called with his parent node. Finally, it needs to be saved to a file.
1 Public Static voidRemovexml () {2Saxbuilder SB =NewSaxbuilder ();3Document doc =NULL;4 Try {5doc = Sb.build ("Person.xml");6Element root =doc.getrootelement ();7list<element> list = Root.getchildren ("Person");8 for(Element el:list) {9 if(El.getattributevalue ("id"). Equals ("ID001"))) {Ten root.removecontent (EL); One } A } -}Catch(Exception e) { - e.printstacktrace (); the } - SaveXML (DOC); -}
"Java Development Series"--jdom Create, modify, delete, and read XML files