JDOM XML Parser

  JDOM provides a way to represent that document for easy and efficient reading, manipulation, and writing. It’s an alternative(替代) to DOM and SAX.

JDOM官网: http://jdom.org/
GitHub: https://github.com/hunterhacker/jdom/

JDOM是第三方jar:

Gradle:

compile group: 'org.jdom', name: 'jdom2', version: '2.0.6'

Maven:

<dependency>
    <groupId>org.jdom</groupId>
    <artifactId>jdom2</artifactId>
    <version>2.0.6</version>
</dependency>
1.0 How to read XML file in Java – (JDOM Parser)
<?xml version="1.0"?>
<company>
    <staff>
        <firstname>yong</firstname>
        <lastname>mook kim</lastname>
        <nickname>mkyong</nickname>
        <salary>100000</salary>
    </staff>
    <staff>
        <firstname>low</firstname>
        <lastname>yin fong</lastname>
        <nickname>fong fong</nickname>
        <salary>200000</salary>
    </staff>
</company>
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class ReadXMLFile {
    public static void main(String[] args) {

      SAXBuilder builder = new SAXBuilder();
      File xmlFile = new File("c:\\file.xml");

      try {
        Document document = (Document) builder.build(xmlFile);
        Element rootNode = document.getRootElement();
        List list = rootNode.getChildren("staff");

        for (int i = 0; i < list.size(); i++) {

           Element node = (Element) list.get(i);

           System.out.println("First Name : " + node.getChildText("firstname"));
           System.out.println("Last Name : " + node.getChildText("lastname"));
           System.out.println("Nick Name : " + node.getChildText("nickname"));
           System.out.println("Salary : " + node.getChildText("salary"));

        }

      } catch (IOException io) {
        System.out.println(io.getMessage());
      } catch (JDOMException jdomex) {
        System.out.println(jdomex.getMessage());
      }
    }
}

Output

First Name : yong
Last Name : mook kim
Nick Name : mkyong
Salary : 100000
First Name : low
Last Name : yin fong
Nick Name : fong fong
Salary : 200000
2.0 How to modify XML file in Java – (JDOM Parser)

File : file.xml – Original XML file.

<?xml version="1.0" encoding="UTF-8"?>
<company>
  <staff id="1">
    <firstname>yong</firstname>
    <lastname>mook kim</lastname>
    <nickname>mkyong</nickname>
    <salary>5000</salary>
  </staff>
</company>

File : file.xml – Newly modified XML file.

<?xml version="1.0" encoding="UTF-8"?>
<company>
  <staff id="2">
    <lastname>mook kim</lastname>
    <nickname>mkyong</nickname>
    <salary>7000</salary>
    <age>28</age>
  </staff>
</company>
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class ModifyXMLFile {
    public static void main(String[] args) {

      try {

        SAXBuilder builder = new SAXBuilder();
        File xmlFile = new File("c:\\file.xml");

        Document doc = (Document) builder.build(xmlFile);
        Element rootNode = doc.getRootElement();

        // update staff id attribute
        Element staff = rootNode.getChild("staff");
        staff.getAttribute("id").setValue("2");

        // add new age element
        Element age = new Element("age").setText("28");
        staff.addContent(age);

        // update salary value
        staff.getChild("salary").setText("7000");

        // remove firstname element
        staff.removeChild("firstname");

        XMLOutputter xmlOutput = new XMLOutputter();

        // display nice nice
        xmlOutput.setFormat(Format.getPrettyFormat());
        xmlOutput.output(doc, new FileWriter("c:\\file.xml"));

        // xmlOutput.output(doc, System.out);

        System.out.println("File updated!");
      } catch (IOException io) {
        io.printStackTrace();
      } catch (JDOMException e) {
        e.printStackTrace();
      }
    }
}
3.0 How to create XML file in Java – (JDOM Parser)
import java.io.FileWriter;
import java.io.IOException;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class WriteXMLFile {
    public static void main(String[] args) {

      try {

        Element company = new Element("company");
        Document doc = new Document(company);
        doc.setRootElement(company);

        Element staff = new Element("staff");
        staff.setAttribute(new Attribute("id", "1"));
        staff.addContent(new Element("firstname").setText("yong"));
        staff.addContent(new Element("lastname").setText("mook kim"));
        staff.addContent(new Element("nickname").setText("mkyong"));
        staff.addContent(new Element("salary").setText("199999"));

        doc.getRootElement().addContent(staff);

        Element staff2 = new Element("staff");
        staff2.setAttribute(new Attribute("id", "2"));
        staff2.addContent(new Element("firstname").setText("low"));
        staff2.addContent(new Element("lastname").setText("yin fong"));
        staff2.addContent(new Element("nickname").setText("fong fong"));
        staff2.addContent(new Element("salary").setText("188888"));

        doc.getRootElement().addContent(staff2);

        // new XMLOutputter().output(doc, System.out);
        XMLOutputter xmlOutput = new XMLOutputter();

        // display nice nice
        xmlOutput.setFormat(Format.getPrettyFormat());
        xmlOutput.output(doc, new FileWriter("c:\\file.xml"));

        System.out.println("File Saved!");
      } catch (IOException io) {
        System.out.println(io.getMessage());
      }
    }
}

````

Result:




<div class="se-preview-section-delimiter"></div>

```xml
<?xml version="1.0" encoding="UTF-8"?>
<company>
  <staff id="1">
    <firstname>yong</firstname>
    <lastname>mook kim</lastname>
    <nickname>mkyong</nickname>
    <salary>199999</salary>
  </staff>
  <staff id="2">
    <firstname>low</firstname>
    <lastname>yin fong</lastname>
    <nickname>fong fong</nickname>
    <salary>188888</salary>
  </staff>
</company>

  
JDOM学习相关文档
http://www.studytrails.com/java/xml/jdom2/java-xml-jdom2-introduction/
https://www.journaldev.com/1206/jdom-parser-read-xml-file-object-java

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值