1.xml parsing needs to import Dom4j-full.jar package
Saxreader sax = new Saxreader (); Create Saxreader
Document doc = sax.read ("Address" or new File ("Address")); Read an XML file
Element elemp = Doc.getrootelement (); Get root element
Here if you want to find all the elements below the root element such as:
list<element> list = Elemp.elements (); Create a collection element
map<string,string> map = hasmap<string> (); Set up a Map collection
for (Element cf:list) {
String key = Cf.getname (); Get tag element access as key value
String value = Cf.gettext ();//Gets the text element inside the tag as value
Map.put (Key,value); To deposit values into the map collection
}
How to get the specified element
As shown above, the root element has been obtained.
Element root = element.element ("name"); Gets the specified element
String GetText (); Gets the current text element
Gets the specified element attribute attribute (int index) method
Gets the current element index for index starting from 0
Attribute attr =element.attribute ("name Like name")
2. Creating an XML file
Document doc = Documenthelper.createdocument (); Create Document Object
Add a root element
Element root = doc.addelement ("name"); Name represents the child element names that you want to add
Append child elements
Element ele = root.addelement ("name"); Name represents the child element names that you want to add
Adds the attribute ID value to the newly appended child element to CP;
Ele.addattribute ("id", "CP");
Java Operations Common API