I. How to configure DOM4J
XML parsing: Click to view
Two. Modify Project class content1.) Add: Documents, tags, attributes 2.) Modify: Property value, Text 3.) Deleting: Tags, properties
1.) Add: Documents, tags, attributes
A. Creating text Write.xml
Create text
Document doc = Documenthelper.createdocument ();
B. Creating labels
Create label
element rootelem=doc.addelement ("contactlist");//create root tag
element Contelem = Rootelem.addelement (" Cotact "); Create a child label
Contelem.addattribute (" id "," 001 ");//Add attribute
Element Nameelme = contelem.addelement (" name "); /Increase the
Nameelme.addtext ("John");//Add name
rootelem.addelement ("cotact");//Add Label
C. Writing the document object created to an XML file
3 Write the Document object created to the XML file
//Specify the file output location
fileoutputstream out = new FileOutputStream ("./src/write.xml");
OutputFormat format = Outputformat.createcompactformat ()//Generate physical file, layout is more chaotic for computer
outputformat format = Outputformat.createprettyprint ();//Standardized layout for display when viewing.
//1. Create Write file
format.setencoding ("Utf-8");//Specify file format
XMLWriter writer = new XMLWriter (out,format);
Writer.write (DOC),//write file
System.out.println ("write Success");
Writer.close ();
The results are as follows:
2.) Modify: Attribute value, text
# # #注意: Modify the XML source file One.xml; Write the modified content to Write.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<contactList>
<contact id= "1" >
<name> xxx </name>
<age>20</age>
<phone>15426354785</phone>
<qq> 1024557455</qq>
</contact>
<contact id= "2" >
<name> John </name>
< age>44</age>
<phone>17854213658</phone>
<qq>1024532584</qq>
</contact>
</contactList>
A. Modifying property values
Programme I:
Modify property value The first ID is changed to "003"
Document doc = new Saxreader (). Read ("./src/one.xml");/Get Tag object
Element Contele = Doc.getrootelement (). Element ("contact");/Get attribute
Idatt = contele.attribute ("id");
Idatt.setvalue ("003");
Programme II:
Modify property value The first ID is changed to "004"
//element contEle2 = Doc.getrootelement (). Element ("contact"); Get Attribute Object
//contele2.addattribute ("id", "004");
B. Modifying the text
Modify text 1 get label, 2 Modify text modifies the first name value to Zhang Fei
element nameele = doc.getrootelement (). Element ("contact"). Element ("name" );
Nameele.settext ("Zhang Fei");
3.) Delete: tags, properties
# #注意: Delete the XML source file one.xml content; Write the deleted content to Write.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<contactList>
<contact id= "1" >
<name> xxx </name>
<age>20</age>
<phone>15426354785</phone>
<qq> 1024557455</qq>
</contact>
<contact id= "2" >
<name> John </name>
< age>44</age>
<phone>17854213658</phone>
<qq>1024532584</qq>
</contact>
</contactList>
A. Delete labels
Programme I:
Deletes the First Age label
Element agename = doc.getrootelement (). Element ("contact"). Element (' age '); get the label
. Agename.detach ()//Delete label
Programme II:
Deletes the First Age label
Element agename = doc.getrootelement (). Element ("contact"). Element (' age '); get the label
. Agename.getparent (). Remove (agename);//Get the label's parent tag, and then delete its child labels
B. Delete Properties
Programme I:
Delete 2nd id
Element Contele = (Element) doc.getrootelement (). elements (). get (1);
The Property object attribute
idatt= contele.attribute ("id") is obtained;
Method one
Idatt.detach ();//Delete attribute
Programme II:
Delete the 2nd id attribute
Element contele = (Element) doc.getrootelement (). elements (). get (1);
The Property object attribute
idatt= contele.attribute ("id") is obtained;
Idatt.getparent (). Remove (Idatt);