Java learns forty-three (DOM4j parses XML) and dom4jxml from scratch

Source: Internet
Author: User

Java learns forty-three (DOM4j parses XML) and dom4jxml from scratch
1. Create XML

// Create XML public static void gernatorXML () {// create Document Object Document doc = incluenthelper. createDocument (); // create the root node students Element studentsElement = doc. addElement ("students"); // create a subnode student Element studentElement1 = studentsElement. addElement ("student"); // Add the ID attribute studentelement1.addattriement ("id", "1001"); // Add the subnode studentElement1.addElement ("name "). setText ("Zhang San"); studentElement1.addElement ("age "). setText ("18"); studentElement1.addElement ("sex "). setText ("male"); studentElement1.addElement ("phone "). setText ("13800138000"); studentElement1.addElement ("address "). setText ("Shenzhen Baoan"); // create the same two student nodes Element studentElement2 = studentsElement. addElement ("student"); // Add the ID attribute studentelement2.addattriement ("id", "10012"); // Add the subnode studentElement2.addElement ("name "). setText ("Li Si"); studentElement2.addElement ("age "). setText ("19"); studentElement2.addElement ("sex "). setText ("female"); studentElement2.addElement ("phone "). setText ("13412369854"); studentElement2.addElement ("address "). setText ("Shanghai"); // create the same two student nodes Element studentElement3 = studentsElement. addElement ("student"); // Add the ID attribute studentelement3.addattriement ("id", "10013"); // Add the subnode studentElement3.addElement ("name "). setText ("Wang Wu"); studentElement3.addElement ("age "). setText ("25"); studentElement3.addElement ("sex "). setText ("female"); studentElement3.addElement ("phone "). setText ("13410110104"); studentElement3.addElement ("address "). setText ("Guangdong"); try {// create XML to write the Document into XML // set the format OutputFormat format = OutputFormat. createPrettyPrint (); // you can set the character encoding to gb2312 or gbk format. setEncoding ("UTF-8"); // enter XMLWriter writer = new XMLWriter (new FileWriter ("xml/students. xml "), format); // write the document writer. write (doc); // close the stream writer. close (); // The output prompts System. out. println ("======= successful write ======");} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();}}

 

Ii. parsing XML
// Read XML public static void getXML () {try {// read XML SAXReader reader = new SAXReader (); Document doc = reader. read ("xml/students. xml "); // get the root node Element root = doc. getRootElement (); // obtain the List of child nodes <Element> stulList = root. elements (); // traverse for (Element ele: stulList) {// obtain the attribute String id = ele. attributeValue ("id"); System. out. println ("student ID:" + id); // obtain the text content name String name = ele. elementText ("name"); // age String age = ele. elementText ("age"); // sex String sex = ele. elementTextTrim ("sex"); // phone String phone = ele. elementTextTrim ("phone"); // address String address = ele. elementTextTrim ("address"); System. out. println ("name:" + name + "\ t age:" + age + "\ t Gender:" + sex + "\ t tel: "+ phone +" \ t address: "+ address) ;}} catch (incluentexception e) {e. printStackTrace ();}}

 

3. Add nodes
// Add a new public static void addXML () {try {// read the XML creation parser SAXReader reader = new SAXReader (); // create the Document Object Document doc = reader. read ("xml/students. xml "); // get the root node Element root = doc. getRootElement (); // create a new node Element student = root. addElement ("student"); // Add the property student. addAttribute ("id", "10020"); // create a subnode student. addElement ("name "). setText ("Zhang Sanfeng"); student. addElement ("age "). setText ("108"); student. addElement ("sex "). setText ("male"); student. addElement ("phone "). setText ("13800138001"); student. addElement ("address "). setText ("Wudang Mountains"); // write XML // set the character encoding and format OutputFormat format = new OutputFormat (); format. setEncoding ("UTF-8"); // create the output stream XMLWriter writer = new XMLWriter (new FileWriter ("xml/students. xml "), format); // write to Document writer. write (doc); // close the stream writer. close (); // The System prompt is displayed. out. println ("======== added successfully ======");} catch (entexception e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}

 

4. Modify nodes
// Modify the node content. Based on id10013, set Wang Wu's name to Wang Xiaowei, age 30, Gender: male, tel: 138000138000, address: Beijing public static void modify () {try {// create SAXReader parser SAXReader reader = new SAXReader (); // create Document doc = reader. read ("xml/students. xml "); // find the root node Element root = doc. getRootElement (); // List of subnode sets <Element> studentList = root. elements (); // traverse the search for (Element ele: studentList) {if (ele. attributeValue ("id "). equals ("10013") {ele. element ("name "). setText ("Wang xiaowu"); ele. element ("age "). setText ("30"); ele. element ("sex "). setText ("male"); ele. element ("phone "). setText ("138000138000"); ele. element ("address "). setText ("Beijing") ;}/// write XML // set the character format and encoding OutputFormat format = new OutputFormat (); format. setEncoding ("UTF-8"); XMLWriter writer = new XMLWriter (new FileWriter ("xml/students. xml "), format); // write the Document to writer. write (doc); // close the stream writer. close (); // The System prompt is displayed. out. println ("==== modified successfully ====");} catch (incluentexception e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}

 

5. delete node XML
/Delete the node to delete the public static void delete () {try {// create the SAXReader parser SAXReader reader = new SAXReader (); // create the Document Object Document doc = reader. read ("xml/students. xml "); // get the root node Element root = doc. getRootElement (); // set List <Element> stuList = root. elements (); // traverse for (Element ele: stuList) {if (ele. attributeValue ("id "). equals ("10013") {// obtain the parent node and delete it. getParent (). remove (ele) ;}/// write XML // set the character encoding format OutputFormat format = new OutputFormat (); format. setEncoding ("UTF-8"); // output stream XMLWriter writer = new XMLWriter (new FileWriter ("xml/students. xml "), format); writer. write (doc); writer. close (); System. out. println ("=== deleted successfully ===");} catch (deleentexception e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}

 

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.