You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

视频批注带时间戳XML持久化实现问题(JAXB相关)

解决JAXB保存视频批注与时间戳的问题

嘿,我看到你在做一个双视频播放界面,想把三个文本框的批注和视频时间戳用JAXB保存到XML里,但目前的保存操作可能因为类的小问题没正常工作。咱们一步步来修正:

首先,你的Saved类存在JavaBean规范不匹配的问题

JAXB是严格遵循JavaBean的getter/setter命名规则的,你的几个getter方法命名错误,还有一个返回值写错了,这会导致JAXB无法正确识别要序列化的属性,进而保存的XML可能缺失字段或者直接失败。

修正后的Saved类代码

@XmlRootElement
public class Saved {
    float position;
    String gap;
    String forces;
    String findings;

    public float getPosition() {
        return position;
    }

    @XmlAttribute
    public void setPosition(float position) {
        this.position = position;
    }

    // 修正getter方法名,符合JavaBean规范
    public String getGap() {
        return gap;
    }

    @XmlElement
    public void setGap(String gap) {
        this.gap = gap;
    }

    // 修正方法名为getForces,符合规范
    public String getForces() {
        return forces;
    }

    @XmlElement
    public void setForces(String forces) {
        this.forces = forces;
    }

    // 修正方法名为getFindings,同时返回正确的变量findings
    public String getFindings() {
        return findings;
    }

    @XmlElement
    public void setFindings(String findings) {
        this.findings = findings;
    }
}

为什么要这么改?

  • JavaBean要求getter方法必须以get开头,后面跟属性名的首字母大写形式(比如gap对应getGap()),JAXB靠这个规则来识别类的属性。
  • 原来的findings()方法错误返回了forces变量,这会导致保存的findings字段值和forces一样,完全不符合你的需求。

额外优化建议(可选)

目前你的代码每次点击保存都会覆盖text.xml,只能保存一条批注记录。如果想保存多条,可以创建一个包含List<Saved>的根类,比如:

@XmlRootElement(name = "annotations")
public class AnnotationList {
    private List<Saved> annotations;

    @XmlElement(name = "annotation")
    public List<Saved> getAnnotations() {
        if (annotations == null) {
            annotations = new ArrayList<>();
        }
        return annotations;
    }

    public void setAnnotations(List<Saved> annotations) {
        this.annotations = annotations;
    }
}

然后修改保存按钮的逻辑,先读取现有XML里的记录(如果文件存在),再添加新的批注后保存:

Save.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent i) {
        Saved saved = new Saved();
        saved.setPosition(mediaPlayerComponent2.getMediaPlayer().getPosition() * 1000);
        saved.setGap(textArea.getText());
        saved.setForces(textArea1.getText());
        saved.setFindings(textArea2.getText());

        AnnotationList annotationList = new AnnotationList();
        File file = new File("text.xml");

        // 如果文件已存在,读取现有记录
        if (file.exists()) {
            try {
                JAXBContext jaxbContext = JAXBContext.newInstance(AnnotationList.class);
                Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
                annotationList = (AnnotationList) jaxbUnmarshaller.unmarshal(file);
            } catch (JAXBException e) {
                e.printStackTrace();
            }
        }

        // 添加新的批注
        annotationList.getAnnotations().add(saved);

        // 保存到XML
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(AnnotationList.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            jaxbMarshaller.marshal(annotationList, file);
            jaxbMarshaller.marshal(annotationList, System.out);
            // 可以添加用户提示,比如弹出对话框告知保存成功
            JOptionPane.showMessageDialog(null, "批注保存成功!");
        } catch (JAXBException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "保存失败:" + e.getMessage());
        }
    }
});

这样每次保存都会把新的批注追加到XML里,而不是覆盖原有内容,用户体验更好。

内容的提问来源于stack exchange,提问作者Mark Harrison

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:42:19