杰瑞科技汇

document、xml、java 如何协同工作?

Of course! "Document XML Java" refers to the process of parsing, creating, manipulating, and serializing XML documents in Java. There are two primary, standard ways to do this:

document、xml、java 如何协同工作?-图1
(图片来源网络,侵删)
  1. SAX (Simple API for XML): An event-driven parser. It's fast and memory-efficient because it reads the XML sequentially and doesn't load the whole document into memory. You get notified of "events" like the start of an element, the end of an element, and character data.
  2. DOM (Document Object Model): A tree-based parser. It reads the entire XML document into memory and creates a tree-like structure (a DOM tree) in memory. This allows you to navigate, search, and modify the document in any direction, but it uses more memory, especially for large files.

Additionally, modern Java development often uses higher-level libraries that build on these APIs.

Let's break down each approach with code examples.


The DOM (Document Object Model) Approach

The DOM API is part of the standard Java library (javax.xml.parsers and org.w3c.dom). It's great for smaller XML files where you need to access data in a non-sequential way.

Example XML File (students.xml)

Let's use this file for our examples:

document、xml、java 如何协同工作?-图2
(图片来源网络,侵删)
<?xml version="1.0" encoding="UTF-8"?>
<students>
    <student id="101">
        <name>Alice</name>
        <age>20</age>
        <email>alice@example.com</email>
    </student>
    <student id="102">
        <name>Bob</name>
        <age>22</age>
        <email>bob@example.com</email>
    </student>
</students>

Java Code to Parse XML with DOM

This code will read students.xml and print the name and email of each student.

import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.File;
public class DomParserExample {
    public static void main(String[] args) {
        try {
            // 1. Create a DocumentBuilderFactory
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            // 2. Create a DocumentBuilder
            DocumentBuilder builder = factory.newDocumentBuilder();
            // 3. Parse the XML file and create a Document object
            Document document = builder.parse(new File("students.xml"));
            // 4. Normalize the document (optional but recommended)
            document.getDocumentElement().normalize();
            // 5. Get the root element
            Element root = document.getDocumentElement();
            System.out.println("Root element: " + root.getNodeName());
            // 6. Get all the "student" elements
            NodeList studentList = root.getElementsByTagName("student");
            System.out.println("\n--- Student Information ---");
            // 7. Loop through the student nodes
            for (int i = 0; i < studentList.getLength(); i++) {
                Node node = studentList.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element studentElement = (Element) node;
                    // Get the 'id' attribute
                    String id = studentElement.getAttribute("id");
                    System.out.println("\nStudent ID: " + id);
                    // Get the 'name' element
                    NodeList nameList = studentElement.getElementsByTagName("name");
                    Element nameElement = (Element) nameList.item(0);
                    String name = nameElement.getChildNodes().item(0).getNodeValue();
                    System.out.println("Name: " + name);
                    // Get the 'email' element
                    NodeList emailList = studentElement.getElementsByTagName("email");
                    Element emailElement = (Element) emailList.item(0);
                    String email = emailElement.getChildNodes().item(0).getNodeValue();
                    System.out.println("Email: " + email);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output:

Root element: students
--- Student Information ---
Student ID: 101
Name: Alice
Email: alice@example.com
Student ID: 102
Name: Bob
Email: bob@example.com

The SAX (Simple API for XML) Approach

SAX is a read-only, event-driven parser. It's more complex to use but much more memory-efficient for very large XML files.

Java Code to Parse XML with SAX

We need to create a "handler" that will react to the events fired by the parser.

document、xml、java 如何协同工作?-图3
(图片来源网络,侵删)
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
public class SaxParserExample {
    public static void main(String[] args) {
        try {
            // 1. Create a SAXParserFactory
            SAXParserFactory factory = SAXParserFactory.newInstance();
            // 2. Create a SAXParser
            SAXParser saxParser = factory.newSAXParser();
            // 3. Create our custom handler
            DefaultHandler handler = new DefaultHandler() {
                boolean inName = false;
                boolean inEmail = false;
                // Called when the parser starts parsing an element
                @Override
                public void startElement(String uri, String localName,
                                         String qName, Attributes attributes) throws SAXException {
                    if (qName.equalsIgnoreCase("student")) {
                        System.out.println("\nFound Student. ID: " + attributes.getValue("id"));
                    }
                    if (qName.equalsIgnoreCase("name")) {
                        inName = true;
                    }
                    if (qName.equalsIgnoreCase("email")) {
                        inEmail = true;
                    }
                }
                // Called when the parser ends parsing an element
                @Override
                public void endElement(String uri, String localName, String qName) throws SAXException {
                    // System.out.println("End Element :" + qName);
                }
                // Called to get the text content of an element
                @Override
                public void characters(char ch[], int start, int length) throws SAXException {
                    if (inName) {
                        System.out.println("Name: " + new String(ch, start, length));
                        inName = false;
                    }
                    if (inEmail) {
                        System.out.println("Email: " + new String(ch, start, length));
                        inEmail = false;
                    }
                }
            };
            // 4. Parse the XML file with the handler
            saxParser.parse(new File("students.xml"), handler);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output:

Found Student. ID: 101
Name: Alice
Email: alice@example.com
Found Student. ID: 102
Name: Bob
Email: bob@example.com

Modern Java Libraries (Recommended)

While DOM and SAX are fundamental, modern Java projects often use higher-level libraries that simplify XML handling significantly.

a) JAXB (Java Architecture for XML Binding)

JAXB allows you to convert Java objects to XML (marshalling) and XML to Java objects (unmarshalling) using annotations. This is the most common and "Java-idiomatic" way to handle XML in modern applications.

Step 1: Create Java classes that map to the XML structure.

// File: Student.java
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement // This annotation maps the class to the root XML element
public class Student {
    private int id;
    private String name;
    private int age;
    private String email;
    // No-arg constructor is required by JAXB
    public Student() {}
    @XmlAttribute // Maps this field to an XML attribute
    public int getId() { return id; }
    public void setId(int id) { this.id = id; }
    @XmlElement // Maps this field to an XML element
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    @XmlElement
    public int getAge() { return age; }
    public void setAge(int age) { this.age = age; }
    @XmlElement
    public String getEmail() { return email; }
    public void setEmail(String email) { this.email = email; }
}

Step 2: Create a wrapper class for the list of students.

// File: Students.java
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.List;
@XmlRootElement
public class Students {
    private List<Student> students = new ArrayList<>();
    @XmlElement(name = "student")
    public List<Student> getStudents() {
        return students;
    }
    public void setStudents(List<Student> students) {
        this.students = students;
    }
}

Step 3: Use JAXB to parse the XML.

// File: JaxbParserExample.java
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;
public class JaxbParserExample {
    public static void main(String[] args) {
        try {
            // 1. Create JAXBContext
            JAXBContext jaxbContext = JAXBContext.newInstance(Students.class);
            // 2. Create an Unmarshaller
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            // 3. Unmarshal the XML file into a Java object
            Students students = (Students) jaxbUnmarshaller.unmarshal(new File("students.xml"));
            // 4. Now you can work with the Java objects
            System.out.println("--- Parsed Students ---");
            for (Student student : students.getStudents()) {
                System.out.println("ID: " + student.getId() + ", Name: " + student.getName() + ", Email: " + student.getEmail());
            }
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}

Note: JAXB was part of the standard Java library until Java 8. From Java 9 onwards, it was removed and moved to a separate module (java.xml.bind). You might need to add a dependency if you are using a newer JDK without it (e.g., for Maven: jakarta.xml.bind:jakarta.xml.bind-api).

b) StAX (Streaming API for XML)

StAX is a newer, bidirectional streaming API that combines the best of both worlds: it's more memory-efficient than DOM and easier to program than SAX. You can read and write XML in a forward or backward direction.


Summary: Which one to choose?

Method Pros Cons Best For
DOM - Easy to navigate (tree structure)
- Can modify the document
- Standard library (no extra dependencies)
- High memory usage
- Slower for large files
- Small to medium-sized XML files
- When you need to modify the XML
- When you need to access data in random order
SAX - Very low memory usage
- Fast performance
- Good for large files
- Read-only
- Complex to program (event-driven)
- Can't navigate backwards
- Very large XML files (e.g., logs, databases)
- When you only need to extract specific data once
JAXB - Extremely easy to use (Object-to-XML mapping)
- Clean, maintainable code
- Can both read and write
- Requires extra dependencies (in modern Java)
- Less control over low-level parsing
- Most common Java applications
- When you have a clear Java object model that maps to XML
StAX - Memory efficient like SAX
- Easier to code than SAX
- Bidirectional (can go back)
- Standard library
- More complex than JAXB - When you need a balance between memory efficiency and ease of use
- Processing large files where you need to look back a little
分享:
扫描分享到社交APP
上一篇
下一篇