Dom4j parsing XML

Source: Internet
Author: User

// Read content
Public void read () throws exception {
Saxreader reader = new saxreader ();
Document document = reader. Read ("src/book. xml ");

Element root = Document. getrootelement ();
Element book = (element) root. Elements ("book"). Get (1); // get the second book in XML
String value = book. element ("name"). gettext ();
}
// Read attributes
Public void readattr () throws exception {
Saxreader reader = new saxreader ();
Document document = reader. Read ("src/book. xml ");

Element root = Document. getrootelement ();
Element book = (element) root. Elements ("book"). Get (1); // get the second book in XML
String value = book. element ("name"). attributevalue ("name ");
}
// Add
Public void add () throws exception {
Saxreader reader = new saxreader ();
Document document = reader. Read ("src/book. xml ");

Element book = Document. getrootelement (). element ("book ");
Book. addelement ("price"). settext ("209 ");

Xmlwriter writer = new xmlwriter (New outputstreamwriter (New fileoutputstream ("src/book. xml"), "UTF-8 "));
Writer. Write (document );
Writer. Close ();
}
// Add an element to a specified position
Public void Add2 () throws exception {
Saxreader reader = new saxreader ();
Document document = reader. Read ("src/book. xml ");
Element book = Document. getrootelement (). element ("book ");
List list = book. Elements ();

Element element = incluenthelper. createelement ("price ");
Element. settext ("309 ");

List. Add (2, element );
Outputformat format = outputformat. createprettyprint ();
Format. setencoding ("UTF-8 ");

Xmlwriter writer = new xmlwriter (New fileoutputstream ("src/book. xml"), format );
Writer. Write (document );
Writer. Close ();
}
// Delete
Public void Delete () throws exception {
Saxreader reader = new saxreader ();
Document document = reader. Read ("src/book. xml ");

Element price = Document. getrootelement (). element ("book"). element ("price ");
Price. getparent (). Remove (price );
Outputformat format = outputformat. createprettyprint ();
Format. setencoding ("UTF-8 ");

Xmlwriter writer = new xmlwriter (New fileoutputstream ("src/book. xml"), format );
Writer. Write (document );
Writer. Close ();
}
// Update
Public void Update () throws exception {
Saxreader reader = new saxreader ();
Document document = reader. Read ("src/book. xml ");

Element book = (element) document. getrootelement (). Elements ("book"). Get (1 );
Book. element ("author"). settext ("or dawn ");
Outputformat format = outputformat. createprettyprint ();
Format. setencoding ("UTF-8 ");

Xmlwriter writer = new xmlwriter (New fileoutputstream ("src/book. xml"), format );
Writer. Write (document );
Writer. Close ();
}
// Use XPath to extract data from XML documents
Public void extract () throws exception {
Saxreader reader = new saxreader ();
Document document = reader. Read ("src/book. xml ");

String value = Document. selectsinglenode ("author"). gettext (); // obtain the name of the first author.
}
// Search for usernames and passwords that match the user in user. xml.
@ Test
Public void extract2 () throws exception {
String username = "AAA ";
String Password = "123 ";
Saxreader reader = new saxreader ();
Document document = reader. Read ("src/users. xml ");
Node node = Document. selectsinglenode ("// user [@ username = '" + username + "' and @ Password = '" + password + "']");
If (node = NULL ){
System. Out. println ("Login Failed! ");
} Else {
System. Out. println ("Login successful! ");
}
}

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.