XML JAXB马歇尔不保存到xml文件

时间:2019-05-23 00:32:37

标签: java xml javafx jaxb

我有一个javafx程序,该程序调出文件选择器,以允许用户选择其显示并将其显示在网格视图中,并带有插入的标题,该标题在图像被钉住后会弹出。我将文件路径和标题都保存到不同的数组列表中(现在),我的目标是将它们都保存到xml文件中,以便在重新打开应用程序时可以取消编组,以使图像仍然在那里。现在,我只希望能够将两个字符串保存到xml文件中,然后稍后再查找其余的字符串。目前,我可以无错误地运行代码,直到到达stop方法为止,在该方法中,我尝试保存用户已添加到数组列表的每个图像和标题。

我的JAXB注释:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ImageCap {
    private String filePath;
    private String caption;

    public ImageCap() {
    }

    public ImageCap(String filePath, String caption) {
        this.filePath = filePath;
        this.caption = caption;
    }

    @Override
    public String toString() {
        return "ImageCap{" + "filePath=" + filePath + ", caption=" + caption + '}';
    }

    public String getFilePath() {
        return filePath;
    }

    @XmlElement
        public void setFilePath(String filePath) {
            this.filePath = filePath;
        }

      public String getCaptions() {
            return caption;
        }

      @XmlElement
        public void setCaption(String caption) {
            this.caption = caption;
        }

    }

我要测试的主要对象

public static void main(String[] args) {launch(args);}

  public void start(Stage primaryStage) throws JAXBException{

  final JFXPanel bananarama = new JFXPanel();

  //import the library (read))

  // create the (initial) display
  display.makeBrowseButton(primaryStage);
  display.createDisplay(primaryStage);

  // show user
  primaryStage.show();



}@Override
public void stop() throws JAXBException{
  File file = new File("file.xml");
  JAXBContext jaxbContext = JAXBContext.newInstance(ImageCap.class);
  Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

//this.context = JAXBContext.newInstance(ImageCap.class);
  //Marshaller marshaller = context.createMarshaller();
  for(int i = 0; i < display.filePaths.size(); i++)
{
  ImageCap imageCap = new ImageCap();

   imageCap.setFilePath(display.filePaths.get(i));
   imageCap.setCaption(display.captions.get(i).toString());

System.out.println(display.filePaths.get(i).toString());
   try {

   // output pretty printed
   jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

   jaxbMarshaller.marshal(imageCap, file);


       } catch (JAXBException e) {
   e.printStackTrace();
       }

  }
}



停止命令后,这是我的错误:

    this problem is related to the following location:
        at public void ImageCap.setCaption(java.lang.String)
        at ImageCap
    this problem is related to the following location:
        at private java.lang.String ImageCap.caption
        at ImageCap
    Class has two properties of the same name "filePath"
    this problem is related to the following location:
        at public java.lang.String ImageCap.getFilePath()
        at ImageCap
    this problem is related to the following location:
        at private java.lang.String ImageCap.filePath
        at ImageCap

但它具体地在第81行处中断: JAXBContext jaxbContext = JAXBContext.newInstance(ImageCap.class);

为什么有什么主意?

1 个答案:

答案 0 :(得分:0)

如果您的同名字段具有getter和setter方法,则需要使用XmlAccessType.PROPERTY而不是XmlAccessType.FIELD

    @XmlRootElement
    @XmlAccessorType(XmlAccessType.PROPERTY)
    public static class ImageCap {
        private String filePath;
        private String caption;

...

此外,您将遇到的另一个问题是将getCaption s ()拼写错误为复数(应为getCaption())。 JAXB也将对此抱怨。

最后,通过在循环中封送文件,您将使用当前正在处理的imageCap在同一文件上重写。如果要封送所有imageCap,则需要将它们放在List上,然后封送List。为此,您需要一个新的JAXB模型类,例如:

    @XmlRootElement(name = "myImageCapList")
    class ImageCapList {
        @XmlElement
        List<ImageCap> imageCap;

        public ImageCapList() {}

        public ImageCapList(List<ImageCap> imageCaps) {
            this.imageCap = imageCaps;
        }
    }

,您需要创建一个包含ImageCap对象(List<ImageCap>)列表的对象实例,并将其用作调用jaxbMarshaller.marshal方法的目标,如以下方法:

    public void imageCapsMarshal(List<ImageCap> imageCaps, File outFile) {
        try {
            jaxbMarshaller.marshal(new ImageCapList(imageCaps), outFile);
        } catch (JAXBException e) {
            // HANDLE EXCEPTIONS
        }
    }

此外,您还需要适当地实例化JAXBContext

    jaxbContext = JAXBContext.newInstance(ImageCapList.class);

以下是此游戏的完整工作演示,供您使用:

import java.io.File;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

public class JAXBMarshall {

    private JAXBContext jaxbContext;
    private Marshaller jaxbMarshaller;

    public JAXBMarshall() throws JAXBException {
        jaxbContext = JAXBContext.newInstance(ImageCapList.class);
        jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    }

    public void imageCapsMarshal(List<ImageCap> imageCaps, File outFile) {
        try {
            jaxbMarshaller.marshal(new ImageCapList(imageCaps), outFile);
        } catch (JAXBException e) {
            // HANDLE EXCEPTIONS
        }
    }


    public static void main(String[] args) throws JAXBException {
        JAXBMarshall jaxbMarshaller = new JAXBMarshall();

        File file = new File("file.xml");
        List<ImageCap> imageCaps = IntStream.range(0, 10)
                .mapToObj(idx -> new ImageCap("my/file/path/" + idx, idx + ". The Caption!"))
                .collect(Collectors.toList());
        jaxbMarshaller.imageCapsMarshal(imageCaps, file);
    }

    @XmlRootElement(name = "myImageCapList")
    static class ImageCapList {
        @XmlElement
        List<ImageCap> imageCap;

        public ImageCapList() {}

        public ImageCapList(List<ImageCap> imageCaps) {
            this.imageCap = imageCaps;
        }
    }

    @XmlRootElement
    static class ImageCap {
        @XmlElement
        String filePath;

        @XmlElement
        String caption;

        public ImageCap() {}

        public ImageCap(String filePath, String caption) {
            this.filePath = filePath;
            this.caption = caption;
        }

        @Override
        public String toString() {
            return "ImageCap{" + "filePath=" + filePath + ", caption=" + caption + '}';
        }
    }
}

希望这会有所帮助。