[英]XML JAXB marshall not saving to xml file
我有一個 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);
任何想法為什么?
如果您有同名字段的 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>
( 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 + '}';
}
}
}
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.