c语言编程笔录

首页 >   > 笔记大全

笔记大全

利用dom4j怎么实现操作xml文件中的demo

更新时间:2023-07-11

Dom4j介绍

DOM4J是一种基于Java语言的API,用于处理XML文件。它的设计目标是快速、小巧、灵活,而且易于使用。它能够解析XML文件、创建XML文档、生成XML文档以及对XML文档进行修改。DOM4J提供了灵活的API,能够满足各种类型的XML应用程序的需求。

解析XML文件

解析XML文件是DOM4J的一大特点。下面是解析XML文件的示例代码:

```java import java.io.File; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class Dom4jDemo { public static void main(String[] args) throws Exception { SAXReader reader = new SAXReader(); Document document = reader.read(new File("test.xml")); Element root = document.getRootElement(); System.out.println("Root element :" + root.getName()); Element staff = root.element("staff"); String id = staff.attributeValue("id"); System.out.println("Staff id : " + id); Element firstName = staff.element("firstname"); System.out.println("First Name : " + firstName.getText()); Element lastName = staff.element("lastname"); System.out.println("Last Name : " + lastName.getText()); Element email = staff.element("email"); System.out.println("Email : " + email.getText()); Element salary = staff.element("salary"); System.out.println("Salary : " + salary.getText()); } } ```

创建XML文档

DOM4J也可以用于创建XML文档。下面是创建XML文档的示例代码:

```java import java.io.FileOutputStream; import java.util.Iterator; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.Namespace; import org.dom4j.QName; public class Dom4jDemo { public static void main(String[] args) throws Exception { Document document = DocumentHelper.createDocument(); Element root = document.addElement("cars"); Namespace ns = new Namespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); root.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); QName qname = new QName("schemaLocation", ns); root.addAttribute(qname, "http://www.example.com/cars.xsd"); Element car = root.addElement("car"); car.addAttribute("id", "1"); car.addAttribute("make", "toyota"); car.addAttribute("model", "corolla"); Element year = car.addElement("year"); year.setText("2015"); Element color = car.addElement("color"); color.setText("red"); Element description = car.addElement("description"); description.setText("This is a great car!"); Element owner = car.addElement("owner"); Element name = owner.addElement("name"); name.setText("John Doe"); Element age = owner.addElement("age"); age.setText("25"); FileOutputStream out = new FileOutputStream("cars.xml"); document.write(out); out.close(); } } ```

修改XML文档

DOM4J还可以用于修改XML文档。下面是修改XML文档的示例代码:

```java import java.io.File; import java.io.FileOutputStream; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class Dom4jDemo { public static void main(String[] args) throws Exception { SAXReader reader = new SAXReader(); Document document = reader.read(new File("cars.xml")); Element root = document.getRootElement(); Element car = root.element("car"); Element description = car.element("description"); description.setText("This is a really great car!"); FileOutputStream out = new FileOutputStream("cars.xml"); OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(out, format); writer.write(document); writer.close(); } } ```