Java XML JAXB未保存到XML文件

Java XML JAXB未保存到XML文件,java,xml,javafx,java-8,jaxb,Java,Xml,Javafx,Java 8,Jaxb,我有一个javafx程序,它提供了一个filechooser,允许用户拾取图像并将其显示到网格视图中,其中插入了一个标题,该标题在图像被复制后弹出。我将文件路径和标题保存到不同的ArrayList[目前],我的目标是将两者保存到xml文件中,以便在重新打开应用程序时可以对其进行解组,以便图像仍然存在。现在我只希望能够将这两个字符串保存到一个xml文件中,然后在以后找出其余的字符串。目前,我可以运行我的代码,没有错误,直到我达到我的停止方法,我尝试保存用户添加到数组列表中的每个图像和标题 我的JA

我有一个javafx程序,它提供了一个filechooser,允许用户拾取图像并将其显示到网格视图中,其中插入了一个标题,该标题在图像被复制后弹出。我将文件路径和标题保存到不同的ArrayList[目前],我的目标是将两者保存到xml文件中,以便在重新打开应用程序时可以对其进行解组,以便图像仍然存在。现在我只希望能够将这两个字符串保存到一个xml文件中,然后在以后找出其余的字符串。目前,我可以运行我的代码,没有错误,直到我达到我的停止方法,我尝试保存用户添加到数组列表中的每个图像和标题

我的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();
       }

  }
}



但它在第81行特别截断,即:
JAXBContext-JAXBContext=JAXBContext.newInstance(ImageCap.class)


你知道为什么吗?

如果你有相同名称的字段的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
s,则需要将它们放在
列表中,然后整理
列表。为此,您需要一个新的JAXB模型类,如:

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

        public ImageCapList() {}

        public ImageCapList(List<ImageCap> imageCaps) {
            this.imageCap = imageCaps;
        }
    }
此外,还需要适当地实例化
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 + '}';
        }
    }
}
导入java.io.File;
导入java.util.List;
导入java.util.stream.collector;
导入java.util.stream.IntStream;
导入javax.xml.bind.JAXBContext;
导入javax.xml.bind.JAXBException;
导入javax.xml.bind.Marshaller;
导入javax.xml.bind.annotation.xmlement;
导入javax.xml.bind.annotation.XmlRootElement;
公共类JAXBMarshall{
私有JAXBContext JAXBContext;
二等兵马歇尔;
公共JAXBMarshall()抛出jaxbeexception{
jaxbContext=jaxbContext.newInstance(ImageCapList.class);
jaxbMarshaller=jaxbContext.createMarshaller();
setProperty(Marshaller.JAXB_格式化的_输出,true);
}
公共无效imageCapsMarshal(列出imageCaps,文件输出文件){
试一试{
jaxbMarshaller.marshall(新的ImageCapList(imageCaps),outFile);
}捕获(JAXBEException e){
//处理异常
}
}
公共静态void main(字符串[]args)抛出JAXBEException{
JAXBMarshall jaxbMarshaller=新的JAXBMarshall();
File File=新文件(“File.xml”);
列出imageCaps=IntStream.range(0,10)
.mapToObj(idx->newimagecap(“my/file/path/”+idx,idx+”。标题!”)
.collect(Collectors.toList());
jaxbMarshaller.imageCapsMarshal(imageCaps,文件);
}
@XmlRootElement(name=“myImageCapList”)
静态类ImageCapList{
@XmlElement
列表图像CAP;
公共ImageCapList(){}
公共ImageCapList(列出imageCaps){
this.imageCap=imageCaps;
}
}
@XmlRootElement
静态类ImageCap{
@XmlElement
字符串文件路径;
@XmlElement
字符串标题;
公共ImageCap(){}
公共ImageCap(字符串文件路径、字符串标题){
this.filePath=filePath;
this.caption=标题;
}
@凌驾
公共字符串toString(){
返回“ImageCap{“+”filePath=“+filePath+”,caption=“+caption+'}”;
}
}
}


希望这能有所帮助。

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

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

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

最后,通过在循环中封送文件,可以使用当前处理的
imageCap
对同一文件进行反复重写。如果您想要整理所有
imageCap
s,则需要将它们放在
列表中,然后整理
列表。为此,您需要一个新的JAXB模型类,如:

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

        public ImageCapList() {}

        public ImageCapList(List<ImageCap> imageCaps) {
            this.imageCap = imageCaps;
        }
    }
此外,还需要适当地实例化
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 + '}';
        }
    }
}
导入java.io.File;
导入java.util.List;
导入java.util.stream.collector;
导入java.util.stream.IntStream;
导入javax.xml.bind.JAXBContext;
导入javax.xml.bind.JAXBException;
导入javax.xml.bind.Marshaller;
导入javax.xml.bind.annotation.xmlement;
导入javax.xml.bind.annotation.XmlRootElement;
公共类JAXBMarshall{
私有JAXBContext JAXBContext;
二等兵马歇尔;
公共JAXBMarshall()抛出jaxbeexception{
jaxbContext=jaxbContext.newInstance(ImageCapList.class);
jaxbMarshaller=jaxbContext.createMarshaller();
setProperty(Marshaller.JAXB_格式化的_输出,true);
}
公共无效imageCapsMarshal(列出imageCaps,文件输出文件){
试一试{
jaxbMarshaller.marshall(新的ImageCapList(imageCaps),outFile);
}捕获(JAXBEException e){
//处理异常
}
}
公共静态void main(字符串[]args)抛出JAXBEException{
JAXBMarshall jaxbMarshaller=新的JAXBMarshall();
File File=新文件(“File.xml”);
列出imageCaps