jdom解析xml文件_JDOM编辑XML文件示例

jdom解析xml文件

JDOM provides very neat way to manipulate XML files, using JDOM is very easy and the code looks clean and readable. Earlier we saw how to read XML using JDOM and how to write XML using JDOM. Here we will learn how easily we can edit XML files using JDOM.

JDOM提供了一种非常整洁的方式来处理XML文件,使用JDOM非常容易,并且代码看起来清晰易读。 之前,我们了解了如何使用JDOM读取XML和如何使用JDOM编写XML 。 在这里,我们将学习如何使用JDOM编辑XML文件。

JDOM编辑XML文件 (JDOM Edit XML File)

For this tutorial, we have following employees.xml file.

在本教程中,我们有以下employees.xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<Employees xmlns="https://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>

We want to change following for every Employee element in the XML.

我们想要更改XML中每个Employee元素的跟随。

  1. Update all name element to block letters.

    更新所有名称元素以禁止使用字母。
  2. Append M for Male and F for Female in ID attribute.

    在ID属性中,将M代表男性,将F代表女性。
  3. Remove gender element

    删除性别元素
  4. Add new element salary with default value 1000 for each employee

    为每个员工添加默认值1000的新元素工资

JDOM示例–编辑XML文件 (JDOM Example – Edit XML File)

Here is the java program for above changes.

这是上述更改的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("https://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));
    }

}

Notice the use of namespace for retrieving all the elements. Above program produces following output xml.

注意,使用命名空间来检索所有元素。 上面的程序产生以下输出xml。

employees_new.xml

employees_new.xml

<?xml version="1.0" encoding="UTF-8"?>
<Employees xmlns="https://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>

That’s all for JDOM example to edit XML file in java.

这就是JDOM示例以Java编辑XML文件的全部内容。

翻译自: https://www.journaldev.com/1218/jdom-edit-xml-file-example

jdom解析xml文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值