杰瑞科技汇

Java中如何创建和操作XMLDocument?

Of course! The term Java XmlDocument can refer to a few different things, as Java has evolved its XML handling over the years. I'll cover the most common approaches, from the classic, built-in method to the modern, recommended one.

Java中如何创建和操作XMLDocument?-图1
(图片来源网络,侵删)

Here’s a breakdown of the main ways to work with an XML document in Java:

  1. DOM (Document Object Model) - The classic, built-in approach.
  2. JDOM / DOM4J - Popular, third-party libraries that are often easier to use than standard DOM.
  3. StAX (Streaming API for XML) - A modern, memory-efficient, read-only API.
  4. JAXB (Java Architecture for XML Binding) - The modern approach for binding XML to Java objects (data binding).

Let's explore each one.


The Classic DOM (Document Object Model)

This is the original, built-in Java API for XML, defined in the org.w3c.dom package. It's part of Java Standard Edition (JSE).

How it works: The entire XML document is parsed into a tree-like structure in memory. You can then navigate this tree, add, remove, or modify nodes, and finally write the tree back to an XML file.

Java中如何创建和操作XMLDocument?-图2
(图片来源网络,侵删)

Pros:

  • Built-in: No external libraries needed.
  • Flexible: You can navigate the tree in any direction (parent, children, siblings).
  • Powerful: Allows for full read, write, and modification of the document.

Cons:

  • Memory-intensive: The entire document must be loaded into memory. This is not suitable for very large XML files.
  • Verbosity: The code can be quite verbose and a bit clunky to write.

Example: Parsing and Modifying with DOM

Let's use this sample XML file, users.xml:

<?xml version="1.0" encoding="UTF-8"?>
<users>
    <user id="101">
        <name>Alice</name>
        <email>alice@example.com</email>
    </user>
    <user id="102">
        <name>Bob</name>
        <email>bob@example.com</email>
    </user>
</users>

Code to parse, modify, and save:

Java中如何创建和操作XMLDocument?-图3
(图片来源网络,侵删)
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
public class DomExample {
    public static void main(String[] args) {
        try {
            // 1. Create a DocumentBuilder
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            // 2. Parse the XML file to create a Document object
            Document doc = builder.parse(new File("users.xml"));
            // 3. Modify the Document (add a new attribute to the first user)
            NodeList userList = doc.getElementsByTagName("user");
            Node firstUser = userList.item(0);
            Element firstUserElement = (Element) firstUser;
            firstUserElement.setAttribute("status", "active");
            // 4. Create a new element and add it to the first user
            Element newElement = doc.createElement("role");
            newElement.setTextContent("admin");
            firstUserElement.appendChild(newElement);
            // 5. Write the modified Document back to a file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // Pretty print
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File("modified_users.xml"));
            transformer.transform(source, result);
            System.out.println("XML file modified successfully!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Popular Third-Party Libraries: JDOM & DOM4J

These libraries were created to overcome the verbosity and complexity of the standard DOM API. They provide a more Java-friendly and intuitive API.

JDOM Example

You need to add the JDOM dependency to your project (e.g., in Maven):

<dependency>
    <groupId>org.jdom</groupId>
    <artifactId>jdom2</artifactId>
    <version>2.0.6</version>
</dependency>

Code:

import org.jdom2.*;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class JdomExample {
    public static void main(String[] args) {
        try {
            // 1. Build the document from the file
            SAXBuilder builder = new SAXBuilder();
            Document doc = builder.build(new File("users.xml"));
            // 2. Get the root element
            Element rootElement = doc.getRootElement();
            // 3. Modify the document (much cleaner syntax!)
            Element firstUser = rootElement.getChild("user");
            firstUser.setAttribute("status", "active");
            // 4. Add a new child element
            Element role = new Element("role");
            role.setText("admin");
            firstUser.addContent(role);
            // 5. Save the modified document
            XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());
            xmlOutputter.output(doc, new FileWriter("modified_users_jdom.xml"));
            System.out.println("XML file modified successfully with JDOM!");
        } catch (JDOMException | IOException e) {
            e.printStackTrace();
        }
    }
}

The Modern Approach: StAX (Streaming API for XML)

StAX is a "pull" parser, meaning your code actively pulls events from the parser as it reads the XML file sequentially. It's much more memory-efficient than DOM because it doesn't load the whole document into memory.

Pros:

  • Low memory footprint: Ideal for large XML files.
  • Fast: Processes the file as a stream.
  • Good for read-heavy or transformation tasks.

Cons:

  • Read-only (by default): Not designed for easy modification of existing documents.
  • Stateful: You have to keep track of where you are in the document.

Example: Reading with StAX

import javax.xml.stream.*;
import javax.xml.stream.events.*;
import java.io.FileReader;
public class StaxExample {
    public static void main(String[] args) {
        try {
            // Create a XMLInputFactory
            XMLInputFactory factory = XMLInputFactory.newInstance();
            // Create a XMLEventReader
            XMLEventReader eventReader = factory.createXMLEventReader(new FileReader("users.xml"));
            while (eventReader.hasNext()) {
                XMLEvent event = eventReader.nextEvent();
                if (event.isStartElement()) {
                    StartElement startElement = event.asStartElement();
                    // If we have a start element named "user"
                    if (startElement.getName().getLocalPart().equals("user")) {
                        System.out.println("\nFound User:");
                        // Get all attributes of this element
                        Iterator<Attribute> attributes = startElement.getAttributes();
                        while (attributes.hasNext()) {
                            Attribute attribute = attributes.next();
                            System.out.println("  - " + attribute.getName() + ": " + attribute.getValue());
                        }
                    }
                    if (startElement.getName().getLocalPart().equals("name")) {
                        System.out.print("  - Name: ");
                    }
                    if (startElement.getName().getLocalPart().equals("email")) {
                        System.out.print("  - Email: ");
                    }
                }
                if (event.isCharacters()) {
                    Characters characters = event.asCharacters();
                    System.out.println(characters.getData().trim());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

The Recommended Approach for Data: JAXB (Java Architecture for XML Binding)

JAXB is not a parser in the traditional sense. It's a data binding technology that allows you to automatically convert between XML and Java objects (POJOs). This is the cleanest approach if your XML structure maps directly to a Java object model.

Note: JAXB was part of the Java Standard Edition but was removed in Java 9. It's now a standalone module (Jakarta XML Binding).

Example with JAXB

Define Java classes that map to the XML schema.

import javax.xml.bind.annotation.*;
import java.util.List;
// The root element
@XmlRootElement(name = "users")
@XmlAccessorType(XmlAccessType.FIELD)
public class Users {
    @XmlElement(name = "user")
    private List<User> users;
    // getters and setters...
    public List<User> getUsers() { return users; }
    public void setUsers(List<User> users) { this.users = users; }
}
// The <user> element
@XmlAccessorType(XmlAccessType.FIELD)
public class User {
    @XmlAttribute
    private int id;
    @XmlElement
    private String name;
    @XmlElement
    private String email;
    // getters and setters...
    public int getId() { return id; }
    public void setId(int id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public String getEmail() { return email; }
    public void setEmail(String email) { this.email = email; }
}

Use JAXB to marshal (Java -> XML) and unmarshal (XML -> Java).

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class JaxbExample {
    public static void main(String[] args) {
        try {
            // --- Unmarshal: Read XML into Java objects ---
            File file = new File("users.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(Users.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            Users users = (Users) jaxbUnmarshaller.unmarshal(file);
            System.out.println("Successfully unmarshalled XML to Java objects.");
            for (User user : users.getUsers()) {
                System.out.println("User ID: " + user.getId() + ", Name: " + user.getName());
            }
            // --- Marshal: Write Java objects to XML ---
            User newUser = new User();
            newUser.setId(103);
            newUser.setName("Charlie");
            newUser.setEmail("charlie@example.com");
            users.getUsers().add(newUser);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            jaxbMarshaller.marshal(users, new File("users_from_jaxb.xml"));
            System.out.println("\nSuccessfully marshalled Java objects to XML.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Summary and Recommendation

Method Best For Pros Cons
DOM Small to medium documents that need full read/write/modify capabilities. Built-in, flexible, powerful. Memory-heavy, verbose code.
JDOM/DOM4J General purpose XML manipulation where you can use external libraries. Clean, intuitive API, powerful. Requires adding a dependency.
StAX Very large XML files where memory is a concern, or for read-only/transform tasks. Very low memory, fast, streaming. Not for easy modification, stateful.
JAXB Applications where XML maps directly to Java objects (data binding). Extremely clean code, automatic conversion. Requires defining Java classes, not for arbitrary XML structures.

Final Recommendation:

  • For simple, one-off tasks or environments where you can't add libraries, use the classic DOM.
  • For most general-purpose XML manipulation in modern applications, JDOM or DOM4J are excellent choices due to their ease of use.
  • If you are working with huge XML files (e.g., logs, large datasets), use StAX.
  • If your XML is a data format that corresponds directly to your application's data model, JAXB is by far the best and most maintainable solution.
分享:
扫描分享到社交APP
上一篇
下一篇