简体   繁体   English

C#XmlSerializer的XmlInclude在Java中等效

[英]C# XmlSerializer's XmlInclude equivalent in Java

I looking for a Java annotation which does the same like XmlInclude does in C#. 我在寻找Java注释,它与XmlInclude在C#中的作用相同。

I get a XML structure over a socket. 我在套接字上得到了XML结构。 The structure looks like this: 结构如下:

<Answer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <FunctionReturnCode>0</FunctionReturnCode>
    <AnswerObject xsi:type="Status">
       <DoorOpen>79</DoorOpen>
    </AnswerObject>
</Answer>

The corresponding Java Class is defined as follows: 相应的Java类定义如下:

@XmlRootElement(name="Answer")
@XmlType(propOrder = {"functionReturnCode", "answerObject"})
public class Answer 
{
    private Object m_answerObject         = null;
    private long   m_uiFunctionReturnCode = 0;


    @XmlElement(name="FunctionReturnCode")
    public long getFunctionReturnCode(){ return this.m_uiFunctionReturnCode; }
    public void setFunctionReturnCode(long _uiFunctionReturnCode) { this.m_uiFunctionReturnCode = _uiFunctionReturnCode; }

    @XmlElement(name="AnswerObject")
    public Object getAnswerObject() { return this.m_answerObject; }
    public void setAnswerObject(Object _answerObject) { this.m_answerObject = _answerObject;}
}

In C# the class looks something like this: 在C#中,类看起来像这样:

[XmlInclude(typeof(SelStatus))]

<<< this seems to me the magic point public class Answer : ICloneable <<<在我看来这是公共课程的魔点答案:ICloneable

{

    private uint   m_uiFunctionReturnCode  = 0;

    private object m_answerObject          = null;

    .....Setters/Getters here as well
}

The problem is, that "AnswerObject" can be any type of object. 问题是“ AnswerObject”可以是任何类型的对象。 In my example "AnswerObject" is an object of type "Status", but it can be as well a string or what ever. 在我的示例中,“ AnswerObject”是类型为“ Status”的对象,但它也可以是字符串或任何其他形式。

In C# I can use XmlSerializer to de-serialize the XML structure. 在C#中,我可以使用XmlSerializer对XML结构进行反序列化。 In Java I use the following : 在Java中,我使用以下代码:

JAXBContext context = JAXBContext.newInstance(Answer.class);
Unmarshaller unmarschaller = context.createUnmarshaller();

C# and Java (as well) handles strings automatically. C#和Java(同样)自动处理字符串。 But in the case there is another object then a string I can announce C# with XmlInclude other known classes. 但是在有另一个对象的情况下,我可以使用XmlInclude和其他已知类声明一个字符串。 Is there something similar in Java? Java中有类似的东西吗?

@XmlElement on a property of type Object is the correct mapping: 类型为Object的属性上的@XmlElement是正确的映射:

@XmlElement(name="AnswerObject")
public Object getAnswerObject() { return this.m_answerObject; }
public void setAnswerObject(Object _answerObject) { this.m_answerObject = _answerObject;}

You will need to make you ensure that your JAXBContext is aware of any possible values for AnswerObject. 您需要确保JAXBContext知道AnswerObject的所有可能值。 You can make your JAXBContext are of these classes when you create it: 创建JAXBContext时,可以使它们属于这些类:

JAXBContext.newInstance(Answer.class, ValueA.class, ValueB.class);

Or specify them through the @XmlSeeAlso annotation. 或通过@XmlSeeAlso批注指定它们。

@XmlRootElement(name="Answer")
@XmlType(propOrder = {"functionReturnCode", "answerObject"})
@XmlSeeAlso{{ValueA.class, ValueB.class}
public class Answer 
{
    ...
}

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

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