即使从JAXB中的XmlAdapter返回null之后,也不会从XML中删除标记

时间:2018-08-30 21:11:43

标签: java jaxb jaxb2

  1. 我想从XML中删除空标签。
  

<员工>           授予           <姓>           

因此,在上面的XML中,lastName变为空白,并且当前以XML中的Empty标签表示。 但是我想要以下格式的XML:

  

<员工>           授予           

  1. 因此,如下扩展XmlAdapter:

    package com.jaxb.domain;
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    import org.springframework.util.StringUtils;
    
    public class NullifyAdapter extends XmlAdapter<String, String> {
    
      @Override
      public String unmarshal(String strValue) throws Exception {
        // TODO Auto-generated method stub
        return strValue;
      }
    
      @Override
      public String marshal(String strValue) throws Exception {
        if (StringUtils.isEmpty(strValue)) {
          return null;
        }
        return strValue;
      }
    
    }
    
  2. 在package-info.java中将其注册为软件包级别,如下所示:

    @XmlJavaTypeAdapters({
    @XmlJavaTypeAdapter(value=NullifyAdapter.class, type=String.class)
    })
    package com.jaxb.domain;
    
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;
    
  3. 在上述更改之后,生成的XML如下所示:

  

<员工>           授予                      

  1. 在我的jaxb绑定类中,该字段定义为:

    @XmlElement(name = "LastName")
    protected String lastName;
    

请提出建议,如何彻底删除lastName?我需要删除很多字段,因此希望在相似的行中找到解决方案。

1 个答案:

答案 0 :(得分:0)

您可以使用#include <vorbis/vorbisfile.h> #include <iostream> #include <cassert> #include <fstream> #include <iomanip> #include <vector> size_t read(void* buffer, size_t elementSize, size_t elementCount, void* dataSource) { assert(elementSize == 1); std::ifstream& stream = *static_cast<std::ifstream*>(dataSource); stream.read(static_cast<char*>(buffer), elementCount); const std::streamsize bytesRead = stream.gcount(); stream.clear(); // In case we read past EOF return static_cast<size_t>(bytesRead); } int seek(void* dataSource, ogg_int64_t offset, int origin) { static const std::vector<std::ios_base::seekdir> seekDirections{ std::ios_base::beg, std::ios_base::cur, std::ios_base::end }; std::ifstream& stream = *static_cast<std::ifstream*>(dataSource); stream.seekg(offset, seekDirections.at(origin)); stream.clear(); // In case we seeked to EOF return 0; } long tell(void* dataSource) { std::ifstream& stream = *static_cast<std::ifstream*>(dataSource); const auto position = stream.tellg(); assert(position >= 0); return static_cast<long>(position); } int main() { // Open file stream std::ifstream stream; stream.open("C:\\path\\to\\file.ogg", std::ios::binary); OggVorbis_File file; const ov_callbacks callbacks{read, seek, nullptr, tell}; int result = ov_open_callbacks(&stream, &file, nullptr, 0, callbacks); if (result < 0) { std::cout << "Error opening file: " << result << std::endl; return 0; } // Read file info vorbis_info* vorbisInfo = ov_info(&file, -1); std::cout << "File info: " << vorbisInfo->rate << "Hz, " << vorbisInfo->channels << " channels" << std::endl; // Close file ov_clear(&file); } 设置XML的LastName元素定义。
这将使生成的required=true属性用lastName注释。
这样,如果元素为null,JAXB不会对其进行序列化。