JDOM add, delete, modify, and query XML files)
Package bean;
Import java. Io. fileinputstream;
Import java. Io. filenotfoundexception;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. outputstream;
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 xmlparse {
// Parse the XML file
Public static void xmlparse () throws jdomexception, ioexception {
Saxbuilder builder = new saxbuilder ();
Inputstream file = new fileinputstream ("src/XML/PO. xml ");
Document document = builder. Build (File); // obtain the Document Object
Element root = Document. getrootelement (); // obtain the root node
List <element> List = root. getchildren ();
For (Element E: List ){
System. Out. println ("ID =" + E. getattributevalue ("ID "));
System. Out. println ("username =" + E. getchildtext ("username "));
System. Out. println ("Password =" + E. getchildtext ("password "));
}
}
// Add
Public static void addxml () throws jdomexception, filenotfoundexception, ioexception {
Saxbuilder builder = new saxbuilder ();
Document Doc = builder. Build ("src/XML/PO. xml"); // obtain the Document Object
Element root = Doc. getrootelement (); // obtain the root node
// Add new elements
Element element = new element ("person ");
Element. setattribute ("ID", "3 ");
Element element1 = new element ("username ");
Element1.settext ("zhangdaihao ");
Element element2 = new element ("password ");
Element2.settext ("Mima ");
Element. addcontent (element1 );
Element. addcontent (element2 );
Root. addcontent (element );
Doc. setrootelement (Root );
// File Processing
Xmloutputter out = new xmloutputter ();
Out. Output (Doc, new fileoutputstream ("src/XML/PO. xml "));
}
// Delete a node based on its ID
Public static void deleteperson (int id) throws jdomexception, ioexception {
Saxbuilder builder = new saxbuilder ();
Inputstream file = new fileinputstream ("src/XML/PO. xml ");
Document Doc = builder. Build (File); // obtain the Document Object
Element root = Doc. getrootelement (); // obtain the root node
List <element> List = root. getchildren ();
For (Element E: List ){
// Obtain the id value
If (integer. parseint (E. getattributevalue ("ID") = ID ){
Root. removecontent (E );
Break ;//??
}
}
// File Processing
Xmloutputter out = new xmloutputter ();
Out. Output (Doc, new fileoutputstream ("src/XML/PO. xml "));
}
// Modify a node based on its ID
Public static void updateperson (int id) throws jdomexception, ioexception {
Saxbuilder builder = new saxbuilder ();
Inputstream file = new fileinputstream ("src/XML/PO. xml ");
Document Doc = builder. Build (File); // obtain the Document Object
Element root = Doc. getrootelement (); // obtain the root node
List <element> List = root. getchildren ();
For (Element E: List ){
// Obtain the id value
If (integer. parseint (E. getattributevalue ("ID") = ID ){
System. Out. println ("--------------------");
E. getchild ("username"). settext ("111111111 ");
E. getchild ("password"). settext ("password ");
}
}
// File Processing
Xmloutputter out = new xmloutputter ();
Out. Output (Doc, new fileoutputstream ("src/XML/PO. xml "));
}
Static public void main (string ARS []) throws jdomexception, ioexception {
// Addxml (); // Add XML
// Deleteperson (3); // Delete XML
// Updateperson (2); // modify XML
// Xmlparse (); // Parse XML
}
}
Bytes ------------------------------------------------------------------------------------------------------
<? XML version = "1.0" encoding = "UTF-8"?>
<Root>
<Person id = "1">
<Username> Zhang San </username>
& Lt; password & gt; 123123 & lt;/password & gt;
</Person>
<Person id = "2">
<Username> 1111111112 </username>
<Password> password2 </password>
</Person>
</Root>