1

我正在制作一个视频转换器作为一个假期项目。我计划使用 XML 文件来表示待处理的任务,以便用户可以保存任务。这是它的样子:

<?xml version="1.0" ?>
<task-list>
    <task>
        <input-location> </input-location>
        <output-location> </output-location>
        <bit-rate> </bit-rate>
        <output-width> </output-width>
        <output-height> </output-height>
        <target-device> </target-device>
    </task>
    .
    .
    .
    .
</task-list>  

现在,这就是我的想法:
1. 将其解析为org.w3c.dom.Document
2. 使用 XPath 获取NodeList将包含所有内容<task>及其子项的内容。
3. 创建一个Vector<Task>whereTask是一个自定义类。每个Task对象都会包含一个4. 在与每个对象关联org.w3c.dom.Node
的上下文中使用 XPath来获取相关信息。 NodeTask

但这里有一个问题:我如何改变Node? 也许用户想要更改输出位置。我将不得不对相关Task对象的Node,进行更改org.w3c.dom.Document(它还保存了已保存文件中的节点副本)并将其保存到文件中(为了下次运行转换器时的一致性)。
使用 XPath 会变得非常麻烦。XPath 有利于数据“恢复”,但不适合改变,我想。

公共课任务

public class Task{
    Node taskInfo;
    JProgressBar progressBar; // Visual feedback to the user
    .
    .
    .
    .
    .
    // getters and setters
}  

我可以让 getter 使用 XPath 工作。二传手是问题所在。

4

2 回答 2

1

您可以对任何JAXB (JSR-222)实现执行以下操作。从 Java SE 6 开始,JDK/JRE 中包含一个实现:

JAVA模型

任务列表

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlRootElement(name="task-list")
@XmlAccessorType(XmlAccessType.FIELD)
public class TaskList {

    @XmlElement(name="task")
    private List<Task> tasks;

}

任务

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Task {

    @XmlElement(name="input-location")
    private String inputLocation;

    @XmlElement(name="output-location")
    private String outputLocation;

    @XmlElement(name="bit-rate")
    private int bitRate;

    @XmlElement(name="output-width")
    private int outputWidth;

    @XmlElement(name="output-height")
    private int outputHeight;

    @XmlElement(name="target-device")
    private String targetDevice;

}

演示代码

演示

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(TaskList.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum16579050/input.xml");
        TaskList taskList = (TaskList) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(taskList, System.out);
    }

}

输入.xml/输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<task-list>
    <task>
        <input-location>foo input</input-location>
        <output-location>foo output</output-location>
        <bit-rate>1</bit-rate>
        <output-width>2</output-width>
        <output-height>3</output-height>
        <target-device>foo</target-device>
    </task>
    <task>
        <input-location>bar input</input-location>
        <output-location>bar output</output-location>
        <bit-rate>4</bit-rate>
        <output-width>5</output-width>
        <output-height>6</output-height>
        <target-device>bar</target-device>
    </task>
</task-list>
于 2013-05-16T15:13:10.717 回答
1
  1. 看看Apache Digester
  2. 为您的 XML 创建一个模式并将该模式​​处理为类(将被 JAXB 注释),然后 XML 将更容易成为 java 对象(JAXB 文档将说明一切)
于 2013-05-16T04:36:11.287 回答