简体   繁体   English

XML 解组为 java 个对象

[英]XML unmarshalling to java objects

XML response XML 回复

    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
- <Film>
  <film_id>1</film_id> 
  <title>ACADEMY DINOSAUR</title> 
  <description>xxx</description> 
  <length>86</length> 
  <image_id>1</image_id> 
-  <image>
   <image_id>1</image_id> 
   <name>1.jpg</name> 
   <size>408307</size> 
   <type>.jpg</type> 
   <content>base64 byte</content>
   </image>
 </Film>

I have created pojo classes for Film and Image.我为电影和图像创建了 pojo 类。

Film.java电影.java

public class Film {
    private String film_id;
    private String title;
    private String description;
    private String length;
    private String image_id;
    private Image image;
//setter and getter methods
}

Image.java图片.java

public class Image {
    private int image_id;
    private String name;
    private int size;
    private String type;
    private byte[] content;
//setter and getter methods
}

Please help I am new to this and I should use JAXB.请帮助我是新手,我应该使用 JAXB。

SOLUTION:解决方案:

I have added我已经添加了

@XmlRootElement(name = "film")
public class Film {
    private int film_id;
    private String title;
    private String description;
    private int length;
    private int image_id;
    private Image image;
}

on the getter method of Image, I have added @XMLElement(name="image").在Image的getter方法上,我添加了@XMLElement(name="image")。

On Image class I have added an annotation @XmlRootElement(name = "image"), which gives me what I wanted.在图像 class 上,我添加了一个注释 @XmlRootElement(name = "image"),它给了我想要的东西。

The only thing required to get your use case to work is to add an @XmlRootElement annotation on the Film class:让您的用例工作所需的唯一事情是在Film class 上添加@XmlRootElement注释:

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="Film")
public class Film {
    private String film_id;
    private String title;
    private String description;
    private String length;
    private String image_id;
    private Image image;
}

The Other Part of Your Solution解决方案的另一部分

on the getter method of Image, I have added @XMLElement(name="image").在Image的getter方法上,我添加了@XMLElement(name="image")。

On Image class I have added an annotation @XmlRootElement(name = "image"), which gives me what I wanted.在图像 class 上,我添加了一个注释 @XmlRootElement(name = "image"),它给了我想要的东西。

Neither of these steps are required to map your particular use case. map 您的特定用例不需要这些步骤。

Try converting your arrays to list on dublicating classes (worked for me)尝试将您的 arrays 转换为复制类列表(为我工作)

As i see byte[] -> List < Byte>如我所见 byte[] -> List < Byte>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM