JDOM for XML files (add, delete, modify, query)

Source: Internet
Author: User
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>

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.