栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 前沿技术 > 人工智能 > NLP

Java&Xml教程(七)使用JDOM修改XML文件内容

NLP 更新时间:发布时间: 百科书网 趣学号

JDOM提供了非常灵活的方式操作XML文件,使用JDOM非常简单而且代码简洁可读性强。前面我们学习了如何使用JDOM解析XML文件,本节介绍如何使用JDOM修改XML文件内容。

在这个教程中,我们准备对下面的XML文件进行修改:
employees.xml

      25    Pankaj    Male    Java Developer        34    Mona    Female    Manager        45    Dave    Male    Support  

我们将改变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 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

      25    PANKAJ    Java Developer    1000        34    MONA    Manager    1000        45    DAVE    Support    1000  

JDOM提供了非常灵活的方式操作XML文件,使用JDOM非常简单而且代码简洁可读性强。前面我们学习了如何使用JDOM解析XML文件,本节介绍如何使用JDOM修改XML文件内容。
在这个教程中,我们准备对下面的XML文件进行修改:
employees.xml

      25    Pankaj    Male    Java Developer        34    Mona    Female    Manager        45    Dave    Male    Support  

我们将改变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 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

      25    PANKAJ    Java Developer    1000        34    MONA    Manager    1000        45    DAVE    Support    1000  

以上就是Java&Xml教程(七)使用JDOM修改XML文件内容的内容,更多相关内容请关注PHP中文网(www.iotsi.net)!

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/794825.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号