如何有效管理各种转换任务?

时间:2013-05-16 04:32:08

标签: java dom xpath video-processing

我正在制作视频转换器作为假日项目。我计划使用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>,其中Task是自定义类。每个Task对象都包含org.w3c.dom.Node
4.在与每个Node对象关联的Task的上下文中使用XPath以获取相关信息。

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

公共类任务

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

我可以使用XPath使getter工作。塞特斯是问题所在。

2 个答案:

答案 0 :(得分:1)

  1. 查看Apache Digester
  2. 为您的XML创建一个模式,并将该模式​​处理成类(将被JAXB注释),然后XML将更容易成为Java对象(JAXB文档将告诉所有内容)

答案 1 :(得分: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;

}

DEMO CODE

<强>演示

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);
    }

}

<强> input.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>
相关问题