jaxb避免对象返回null
我需要将下面的XML转换为POJO。我无法修改XML。我的问题是upsert对象始终为空。
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<MarginCall xmlns="http://my-name-space" type="Upsert">
<upsert>
<AccountId>ABCD</AccountId>
</upsert>
</MarginCall>
Margincall类
@XmlRootElement(name="MarginCall", namespace="http://my-name-space")
@XmlAccessorType(XmlAccessType.FIELD)
public class MarginCall {
@XmlAttribute
private String type;
@XmlElement
protected Upsert upsert;
// Getters, setters and ToString()
UPSERT类
@XmlType(name = "upsert", namespace="")
@XmlAccessorType(XmlAccessType.FIELD)
public class Upsert {
@XmlElement(name="AccountId")
private String accountId;
// Getter, setter and ToString()
如何转换XML(接收到字符串:下面的消息),
JAXBContext jaxbContext;
jaxbContext = JAXBContext.newInstance(new Class[]{com.sandrew.MarginCall.class});
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
MarginCall marginCall = (MarginCall) jaxbUnmarshaller.unmarshal(new StringReader(message));
System.out.println(marginCall); // prints: MarginCall{type='Upsert', upsert=null} <---- PROBLEM
我尝试更改,删除UpSert上的名称空间,而不会运气。我还创建了一个margincall对象,然后尝试将其写入XML,并确认结果是按照我想要的:
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
Upsert upsert = new Upsert();
upsert.setAccountId("ABCD");
MarginCall m = new MarginCall();
m.setType("T");
m.setUpsert(upsert);
jaxbMarshaller.marshal(m, new File("res.xml"));
// res.xml = <?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns2:MarginCall xmlns:ns2="http://my-name-space" type="T"><upsert><AccountId>ABCD</AccountId></upsert></ns2:MarginCall>
知道为什么当我从字符串到Pojo时,我会在UPSERT对象上获得null?
I need to transform the XML below to a POJO. I cannot modify the XML. My problem is the upsert object is always null.
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<MarginCall xmlns="http://my-name-space" type="Upsert">
<upsert>
<AccountId>ABCD</AccountId>
</upsert>
</MarginCall>
MarginCall Class
@XmlRootElement(name="MarginCall", namespace="http://my-name-space")
@XmlAccessorType(XmlAccessType.FIELD)
public class MarginCall {
@XmlAttribute
private String type;
@XmlElement
protected Upsert upsert;
// Getters, setters and ToString()
Upsert Class
@XmlType(name = "upsert", namespace="")
@XmlAccessorType(XmlAccessType.FIELD)
public class Upsert {
@XmlElement(name="AccountId")
private String accountId;
// Getter, setter and ToString()
How I transform the XML (received as String:message below)
JAXBContext jaxbContext;
jaxbContext = JAXBContext.newInstance(new Class[]{com.sandrew.MarginCall.class});
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
MarginCall marginCall = (MarginCall) jaxbUnmarshaller.unmarshal(new StringReader(message));
System.out.println(marginCall); // prints: MarginCall{type='Upsert', upsert=null} <---- PROBLEM
I tried to change, remove the namespace on Upsert, without luck. I have also created a MarginCall object, then tried to write it as an XML, and confirmed the result is structured as I want:
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
Upsert upsert = new Upsert();
upsert.setAccountId("ABCD");
MarginCall m = new MarginCall();
m.setType("T");
m.setUpsert(upsert);
jaxbMarshaller.marshal(m, new File("res.xml"));
// res.xml = <?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns2:MarginCall xmlns:ns2="http://my-name-space" type="T"><upsert><AccountId>ABCD</AccountId></upsert></ns2:MarginCall>
Any idea why when I go from String to POJO, I get null on the upsert object ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在XML中,名称空间定义
XMLNS =“ http:// my-name-space”
在根元素中,隐式继承嵌套元素。
但是,在Java中,这种隐式继承并非如此。
您需要明确编码。
因此,您还需要为属性指定此名称空间
代表非根元素。
您可以通过指定其名称空间
@xmlelement
注释。中
在类
margincall
In XML the namespace definition
xmlns="http://my-name-space"
in the root element implicitly inherits to the nested elements.
However, in Java this implicit inheriting is not the case.
You need to code this explicitly.
Therefore you need to specify this namespace also for the properties
representing the non-root elements.
You do this by specifying the namespace in their
@XmlElement
annotations.In class
MarginCall
In class
Upsert
After having done this, the output of the unmarshalled object will be