简体   繁体   English

XML到Java JaxB

[英]XML to Java JaxB

My attempt to convert an XML to Java using JAXB not working as expected. 我试图使用JAXB将XML转换为Java的尝试无法正常工作。 There are multiple other similar questions around it but none of the suggested solutions I looked into seem to help me. 周围还有许多其他类似的问题,但是我研究过的建议解决方案似乎都没有帮助。

Below is my bean 下面是我的豆

@XmlRootElement(name = "ListingResponse", namespace = "http://www.random.com")
@XmlType(propOrder = {"success", "listingId", "description"})
public class ListingResponse {
    private String success;
    private String listingId;
    private String description;


    public String getSuccess() {
        return success;
    }

    @XmlElement(name = "Success")
    public void setSuccess(String success) {
        this.success = success;
    }

    public String getListingId() {
        return listingId;
    }

    @XmlElement(name = "ListingId")
    public void setListingId(String listingId) {
        this.listingId = listingId;
    }

    public String getDescription() {
        return description;
    }

    @XmlElement(name = "Description")
    public void setDescription(String description) {
        this.description = description;
    }

Below is my attempt to do the unmarshaling 以下是我尝试解组的尝试

ListingResponse response = null;
try {
    JAXBContext jaxbContext = JAXBContext.newInstance(ListingResponse.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    response = (ListingResponse) jaxbUnmarshaller.unmarshal(new File("response.xml"));

} catch (JAXBException e) {
    e.printStackTrace();
}

And finally my response.xml content 最后是我的response.xml内容

<ListingResponse xmlns="http://www.random.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Success>true</Success>
    <Description>ListingId 123 created.</Description>
    <ListingId>123</ListingId>
</ListingResponse>
  1. There are not exceptions being thrown. 没有异常被抛出。
  2. ' response ' is NOT null. ' response '不为空。
  3. I have tried adding @XmlAccessorType(XmlAccessType.FIELD / PROPERTY) with @XMLEelement annotation on the fields/ set methods but that didn't seem to help either. 我试图在字段/设置方法上添加带有@XMLEelement批注的@XmlAccessorType(XmlAccessType.FIELD / PROPERTY)@XMLEelement似乎也无济于事。

However, response is always 'empty' with none of the fields initialized. 但是,响应始终为“空”,且未初始化任何字段。

Can you guys spot the issue here? 你们能在这里发现问题吗?

Currently you only have specified the correct namespace qualification for the root element. 当前,您仅为根元素指定了正确的名称空间限定。 You need to use the package level @XmlSchema annotation to map the namespace qualification for your model. 您需要使用包级别@XmlSchema批注来映射模型的名称空间限定。

package-info.java package-info.java

@XmlSchema( 
    namespace = "http://www.random.com", 
    elementFormDefault = XmlNsForm.QUALIFIED) 
package example;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

For More Information 欲获得更多信息

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

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