JDOM XML Parser

2024-03-19 12:38
文章标签 xml parser jdom

本文主要是介绍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 attributeElement staff = rootNode.getChild("staff");staff.getAttribute("id").setValue("2");// add new age elementElement age = new Element("age").setText("28");staff.addContent(age);// update salary valuestaff.getChild("salary").setText("7000");// remove firstname elementstaff.removeChild("firstname");XMLOutputter xmlOutput = new XMLOutputter();// display nice nicexmlOutput.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 nicexmlOutput.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

这篇关于JDOM XML Parser的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/826019

相关文章

C#中读取XML文件的四种常用方法

《C#中读取XML文件的四种常用方法》Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具,下面我们就来看看C#中读取XML文件的方法都有哪些吧... 目录XML简介格式C#读取XML文件方法使用XmlDocument使用XmlTextReader/XmlTextWr

在MyBatis的XML映射文件中<trim>元素所有场景下的完整使用示例代码

《在MyBatis的XML映射文件中<trim>元素所有场景下的完整使用示例代码》在MyBatis的XML映射文件中,trim元素用于动态添加SQL语句的一部分,处理前缀、后缀及多余的逗号或连接符,示... 在MyBATis的XML映射文件中,<trim>元素用于动态地添加SQL语句的一部分,例如SET或W

Python xmltodict实现简化XML数据处理

《Pythonxmltodict实现简化XML数据处理》Python社区为提供了xmltodict库,它专为简化XML与Python数据结构的转换而设计,本文主要来为大家介绍一下如何使用xmltod... 目录一、引言二、XMLtodict介绍设计理念适用场景三、功能参数与属性1、parse函数2、unpa

关于Maven中pom.xml文件配置详解

《关于Maven中pom.xml文件配置详解》pom.xml是Maven项目的核心配置文件,它描述了项目的结构、依赖关系、构建配置等信息,通过合理配置pom.xml,可以提高项目的可维护性和构建效率... 目录1. POM文件的基本结构1.1 项目基本信息2. 项目属性2.1 引用属性3. 项目依赖4. 构

使用Python实现批量访问URL并解析XML响应功能

《使用Python实现批量访问URL并解析XML响应功能》在现代Web开发和数据抓取中,批量访问URL并解析响应内容是一个常见的需求,本文将详细介绍如何使用Python实现批量访问URL并解析XML响... 目录引言1. 背景与需求2. 工具方法实现2.1 单URL访问与解析代码实现代码说明2.2 示例调用

intellij idea generatorConfig.xml

generatorConfig.xml <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfigurationPUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-ge

xml概论

以下内容摘录自W3School 一、XML的特性 xml是用来传输和存储数据的,本身对数据没有任何操作。在这里要区别一下html,html是用来显示数据的。xml的焦点是数据内容,html的焦点是数据外观。 下面是xml的定义: •XML 指可扩展标记语言(EXtensible Markup Language) •XML 是一种标记语言,很类似 HTML

XML的创建

这里使用的是org.dom4j的jar包来完成xml格式数据的创建。 import java.io.IOException;import java.io.StringWriter;import org.dom4j.Document;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.

Spring下自定义xml标签

dubbo自定义了很多xml标签,例如<dubbo:application>,那么这些自定义标签是怎么与spring结合起来的呢?我们先看一个简单的例子。 一 编写模型类 1 package com.hulk.testdubbo.model;2 3 public class Hero {4 private String name;5 private int

xml reader

// TODO Auto-generated method stub