【问题标题】:Unmarshaling the nested xml data giving null values解组提供空值的嵌套 xml 数据
【发布时间】:2017-11-28 00:21:41
【问题描述】:
    <bas:person>

             <req:vehicleinfo>
                <!--Zero or more repetitions:-->
                <bas:item>
                   <sch:modelno>k10</sch:modelno>
                   <sch:type>bs4</sch:type>
                </bas:item>
 <bas:item>
                   <sch:modelno>k12</sch:modelno>
                   <sch:type>bs5</sch:type>
                </bas:item>
             </req:extensionInfo>
    </bas:person>

Assume namespace for bas is some http://xxxxx.person.com
 for req is http://xxxxx.request.com
 for bas http://xxxxx.bas.com
for sch http://xxxxx.sch.com

这里是车辆信息,因为我可能有多个值,在我的 Java 对象中,我需要将车辆信息作为数组对某些项目依赖项执行。

这是我的java类

 @XmlRootElement(name="person")
@XmlAccessorType(XmlAccessType.FIELD)
    public class Person implements Serializable{


        @XmlElement(name="vehicleinfo",namespace="http://xxxxx.request.com")
        private VehicleInfo[] vehicleinfo;


// getter & setter

    }

车辆信息

@XmlAccessorType(XmlAccessType.FIELD)
    public class  VehicleInfo{

        @XmlAttribute(name="modelno",namespace="http://xxxxx.sch.com")
        private String key;
        @XmlAttribute(name="type",namespace="http://xxxxx.sch.com")
        private String value;

 //getter and setter

    }

我正在使用以下代码进行解组

SOAPMessage message = MessageFactory.newInstance().createMessage(null,
                new ByteArrayInputStream(xmlInput.getBytes()));
        JAXBContext jaxbContext = JAXBContext.newInstance(Person.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Person person= (Person) jaxbUnmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument());

但是 m 获取车辆信息的键和值作为 nul

【问题讨论】:

  • 你在VehicleInfoPerson中定义了getter和setter吗?
  • 是的!我跳过了问题

标签: java soap jaxb jax-ws-customization


【解决方案1】:

JAXB 本质上不适用于数组,因为 Java 没有动态数组。而是将您的 VehicleInfo 声明为 ArrayList。

【讨论】:

  • 如果我将车辆信息更改为数组列表,上述代码是否可以正常工作
  • @valdim 还是我们需要做一些改变
  • 其实Vehicle Info是一个item的集合。项目有型号和类型。所以你需要另一个类Item。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-21
  • 2021-01-05
  • 2013-06-08
  • 2023-04-02
  • 1970-01-01
  • 2017-02-03
  • 1970-01-01
相关资源
最近更新 更多