java XML JAXB马歇尔未保存到xml文件

deyfvvtc  于 2023-02-02  发布在  Java
关注(0)|答案(1)|浏览(169)

我有一个javafx程序,它会弹出一个filechooser,允许用户选择并将其显示到一个网格视图中,并在选择图像后弹出一个插入的标题。我将文件路径和标题都保存到不同的数组列表中(目前),我的目标是将两者都保存到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();
       }

  }
}

下面是执行stop命令后出现的错误:

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处截止,该线81是:JAXBContext jaxbContext = JAXBContext.newInstance(ImageCap.class);
知道为什么吗

h7appiyu

h7appiyu1#

如果你有相同名称的字段的getter和setter,那么你需要使用XmlAccessType.PROPERTY而不是XmlAccessType.FIELD

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

另外,您将遇到的另一个问题是将getCaptions()拼写为复数,而它应该是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;
        }
    }

您需要创建这个对象的一个示例来 Package 您的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 + '}';
        }
    }
}

Complete code on GitHub

相关问题