简体   繁体   English

Java Jaxb解组数据和数据的字节序

[英]Java Jaxb unmarshall and endianness of data

I am planning to use jaxb to unmarshall xml in to POJO. 我打算使用jaxb将xml解组到POJO中。 Now this pojo will be serialized and sent over network to some other application. 现在,此pojo将被序列化并通过网络发送到其他应用程序。 Now some of this POJO contains a int field (4 or 8 bytes) which when read from xml need to be serialized in either little endian or big endian notation based on some flag. 现在,此POJO中的一些包含一个int字段(4或8个字节),当从xml读取时,需要根据一些标志以little endian或big endian表示法对它进行序列化。 (this flag is not field by field but rather at application instance level). (此标志不是逐字段,而是在应用程序实例级别)。 Is there anyway In Jaxb while unmarshalling can honor this endianness setting by some sort of annotation in xml? 无论如何在Jaxb中都存在,而解组可以通过xml中的某种注释来遵循这种字节序设置吗?

Any help in this regards is appreciated. 在这方面的任何帮助表示赞赏。

Thanks 谢谢

You can specify your own XmlAdapter in your POJO: 您可以在POJO中指定自己的XmlAdapter

@XmlElement(name="intField")
@XmlJavaTypeAdapter(EndianIntAdapter.class)
public int intField;

And then implement the adapter: 然后实现适配器:

class EndianIntAdapter extends XmlAdapter<String, Integer> {
  @Override
  public String marshal(Integer field) throws Exception {
    //int to string based on endianness
  }

  @Override
  public Integer unmarshal(String str) throws Exception {
    //String from xml to int based on endianness
  }
}

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

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