您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關如何使用JDOM修改XML文件內容,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
在這個教程中,我們準備對下面的XML文件進行修改:
employees.xml
<?xml version="1.0" encoding="UTF-8"?><Employees xmlns="http://www.journaldev.com/employees"> <Employee id="1"> <age>25</age> <name>Pankaj</name> <gender>Male</gender> <role>Java Developer</role> </Employee> <Employee id="2"> <age>34</age> <name>Mona</name> <gender>Female</gender> <role>Manager</role> </Employee> <Employee id="3"> <age>45</age> <name>Dave</name> <gender>Male</gender> <role>Support</role> </Employee></Employees>
我們將改變xml中每個Employee元素:
1.修改所有name元素,使它的內容全部變成大寫。
2.在gender(性別)為Male(男)的id屬性值后追加M,gender(性別)為Female(女) 的id屬性值后追加F。
3.刪除gender元素。
4.為每個Employee元素增加salary(薪水)子元素,默認值為1000。
下面是程序代碼:
JDOMXMLEditor.java
package com.journaldev.xml.jdom; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.Namespace; import org.jdom2.input.SAXBuilder; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter; public class JDOMXMLEditor { public static void main(String[] args) throws JDOMException, IOException { final Namespace ns = Namespace.getNamespace("http://www.journaldev.com/employees"); //Get the JDOM document org.jdom2.Document doc = useSAXParser("employees.xml"); //Get list of Employee element Element rootElement = doc.getRootElement(); List<Element> listEmpElement = rootElement.getChildren("Employee", ns); //loop through to edit every Employee element for (Element empElement : listEmpElement) { //change the name to BLOCK letters String name = empElement.getChildText("name", ns); if (name != null) empElement.getChild("name", ns).setText(name.toUpperCase()); //edit the ID attribute based on Gender String gender = empElement.getChildText("gender", ns); if (gender != null && gender.equalsIgnoreCase("female")) { String id = empElement.getAttributeValue("id"); empElement.getAttribute("id").setValue(id + "F"); } else { String id = empElement.getAttributeValue("id"); empElement.getAttribute("id").setValue(id + "M"); } //remove gender element as it's not needed anymore empElement.removeChild("gender", ns); //add salary element with default value to every employee empElement.addContent(new Element("salary", ns).setText("1000")); } //document is processed and edited successfully, lets save it in new file XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat()); //output xml to console for debugging //xmlOutputter.output(doc, System.out); xmlOutputter.output(doc, new FileOutputStream("employees_new.xml")); } //Get JDOM document from SAX Parser private static org.jdom2.Document useSAXParser(String fileName) throws JDOMException, IOException { SAXBuilder saxBuilder = new SAXBuilder(); return saxBuilder.build(new File(fileName)); } }
需要注意的是上面代碼使用命名空間獲取所有元素,運行程序輸出XML文件內容:
employees_new.xml
<?xml version="1.0" encoding="UTF-8"?><Employees xmlns="http://www.journaldev.com/employees"> <Employee id="1M"> <age>25</age> <name>PANKAJ</name> <role>Java Developer</role> <salary>1000</salary> </Employee> <Employee id="2F"> <age>34</age> <name>MONA</name> <role>Manager</role> <salary>1000</salary> </Employee> <Employee id="3M"> <age>45</age> <name>DAVE</name> <role>Support</role> <salary>1000</salary> </Employee></Employees>
JDOM提供了非常靈活的方式操作XML文件,使用JDOM非常簡單而且代碼簡潔可讀性強。前面我們學習了如何使用JDOM解析XML文件,本節介紹如何使用JDOM修改XML文件內容。
在這個教程中,我們準備對下面的XML文件進行修改:
employees.xml
<?xml version="1.0" encoding="UTF-8"?><Employees xmlns="http://www.journaldev.com/employees"> <Employee id="1"> <age>25</age> <name>Pankaj</name> <gender>Male</gender> <role>Java Developer</role> </Employee> <Employee id="2"> <age>34</age> <name>Mona</name> <gender>Female</gender> <role>Manager</role> </Employee> <Employee id="3"> <age>45</age> <name>Dave</name> <gender>Male</gender> <role>Support</role> </Employee></Employees>
我們將改變xml中每個Employee元素:
1.修改所有name元素,使它的內容全部變成大寫。
2.在gender(性別)為Male(男)的id屬性值后追加M,gender(性別)為Female(女) 的id屬性值后追加F。
3.刪除gender元素。
4.為每個Employee元素增加salary(薪水)子元素,默認值為1000。
下面是程序代碼:
JDOMXMLEditor.java
package com.journaldev.xml.jdom; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.Namespace; import org.jdom2.input.SAXBuilder; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter; public class JDOMXMLEditor { public static void main(String[] args) throws JDOMException, IOException { final Namespace ns = Namespace.getNamespace("http://www.journaldev.com/employees"); //Get the JDOM document org.jdom2.Document doc = useSAXParser("employees.xml"); //Get list of Employee element Element rootElement = doc.getRootElement(); List<Element> listEmpElement = rootElement.getChildren("Employee", ns); //loop through to edit every Employee element for (Element empElement : listEmpElement) { //change the name to BLOCK letters String name = empElement.getChildText("name", ns); if (name != null) empElement.getChild("name", ns).setText(name.toUpperCase()); //edit the ID attribute based on Gender String gender = empElement.getChildText("gender", ns); if (gender != null && gender.equalsIgnoreCase("female")) { String id = empElement.getAttributeValue("id"); empElement.getAttribute("id").setValue(id + "F"); } else { String id = empElement.getAttributeValue("id"); empElement.getAttribute("id").setValue(id + "M"); } //remove gender element as it's not needed anymore empElement.removeChild("gender", ns); //add salary element with default value to every employee empElement.addContent(new Element("salary", ns).setText("1000")); } //document is processed and edited successfully, lets save it in new file XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat()); //output xml to console for debugging //xmlOutputter.output(doc, System.out); xmlOutputter.output(doc, new FileOutputStream("employees_new.xml")); } //Get JDOM document from SAX Parser private static org.jdom2.Document useSAXParser(String fileName) throws JDOMException, IOException { SAXBuilder saxBuilder = new SAXBuilder(); return saxBuilder.build(new File(fileName)); } }
需要注意的是上面代碼使用命名空間獲取所有元素,運行程序輸出XML文件內容:
employees_new.xml
<?xml version="1.0" encoding="UTF-8"?><Employees xmlns="http://www.journaldev.com/employees"> <Employee id="1M"> <age>25</age> <name>PANKAJ</name> <role>Java Developer</role> <salary>1000</salary> </Employee> <Employee id="2F"> <age>34</age> <name>MONA</name> <role>Manager</role> <salary>1000</salary> </Employee> <Employee id="3M"> <age>45</age> <name>DAVE</name> <role>Support</role> <salary>1000</salary> </Employee></Employees>
以上就是如何使用JDOM修改XML文件內容,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。