如何将嵌套的xml列表解组回对象[JAXB]?

时间:2019-05-29 23:06:59

标签: java xml javafx jaxb unmarshalling

因此,我有一个javafx程序,该程序允许用户将带有标题的图片上传到网格窗格。当程序关闭时,我保存一个ImageCaptionList,它由ImageCaptions组成,每个ImageCaptions都具有格式为xml格式的文件路径和标题,如下所示(使用JAXB):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ImageCapList>
    <imageCap>
        <caption>caption for back.jpg</caption>
        <filePath>file:/Users/jonathan.rozenberg/Desktop/GFX/back.jpg</filePath>
    </imageCap>
    <imageCap>
        <caption>caption about part1</caption>
        <filePath>file:/Users/jonathan.rozenberg/Desktop/GFX/part1.jpg</filePath>
    </imageCap>
    <imageCap>
        <caption>something about image0</caption>
        <filePath>file:/Users/jonathan.rozenberg/Desktop/GFX/image0.png</filePath>
    </imageCap>
</ImageCapList>

下面我将演示如何设置我的JAXB注释。以及我目前正在尝试解组的方式。我担心的是,如何能够从未编组的xml文档中重新上传带标题的图像。

这是我的ImageCap JAXB注释:

@XmlAccessorType(XmlAccessType.PROPERTY)
public class ImageCap {
  //@XmlElement
    private String filePath;
    //@XmlElement
    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 getCaption() {
            return caption;
        }

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

    }

这是我的ImageCapList JAXB注释[我的根元素]:

@XmlRootElement(name = "ImageCapList")
//@XmlAccessorType(XmlAccessType.FIELD)
  class ImageCapList {
       @XmlElement
       List<ImageCap> imageCap;
       //private String imageCap;

       public ImageCapList() {}

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

我有一个显示类文件,但是它很大,我认为这个问题主要用于编组,因此认为这不是必需的。

这是我的主语

public class JAXBMarshall extends Application {

    private Display display = new Display();
    private MyModel myModel = new MyModel();
    private ImageCap imageCap = new ImageCap();
    private ImageCapList imageCapList = new ImageCapList();

    private JAXBContext jaxbContext;
    private Marshaller jaxbMarshaller;


//private JAXBContext context;
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) {launch(args);}

  public void start(Stage primaryStage) throws JAXBException{

  final JFXPanel bananarama = new JFXPanel();
  File file = new File("file.xml");
  try {
  JAXBContext jaxbContext = JAXBContext.newInstance(ImageCapList.class);
  Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
  ImageCapList imageCaplist = (ImageCapList) jaxbUnmarshaller.unmarshal(file);
  System.out.println(imageCaplist);
  } catch (JAXBException e) {
  e.printStackTrace();
  }


  // 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");
  JAXBMarshall jaxbMarshaller = new JAXBMarshall();




          //File file = new File("file.xml");
          List<ImageCap> imageCaps = IntStream.range(0, display.filePaths.size())
                  .mapToObj(idx -> new ImageCap(display.filePaths.get(idx), display.captions.get(idx).toString()))
                  .collect(Collectors.toList());

                jaxbMarshaller.imageCapsMarshal(imageCaps, file);
              //  }

      }

从解组结束后在主系统中打印我的系统:

ImageCap{[ImageCap{filePath=file:/Users/jonathan.rozenberg/Desktop/GFX/chase4.jpg, caption=feerg}, ImageCap{filePath=file:/Users/jonathan.rozenberg/Desktop/GFX/Green-smoke-background.jpg, caption=gerge}]}

由于其嵌套方式,我不确定如何针对这些特定属性来重新创建图像和标题的图像视图和文本视图。

0 个答案:

没有答案