简体   繁体   English

在 jaxb 编组中删除空的 XML 标记

[英]Remove empty XML tags in jaxb marshalling

How can I avoid empty tags with jaxb marshaller?如何使用 jaxb marshaller 避免空标签?

final Marshaller marshaller = JAXBContext.newInstance(content.getClass()).createMarshaller();

final ByteArrayOutputStream stream = new ByteArrayOutputStream();
final XMLStreamWriter streamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(stream);

final CapitalizedXMLStreamWriter xmlStreamWriter = new CapitalizedXMLStreamWriter(streamWriter);

marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocation);
marshaller.setAdapter(new NullAdapter());
marshaller.marshal(content, xmlStreamWriter);

xmlStreamWriter.flush();
xmlStreamWriter.close();

final String xml = new String(stream.toByteArray());

CapitalizedXMLStreamWriter is my writer that extends StreamWriterDelegate . CapitalizedXMLStreamWriter是我扩展StreamWriterDelegate作家。 It just overrides writeStartElement as this.mDelegate.writeStartElement(arg0, capitalize(arg1));它只是覆盖writeStartElementthis.mDelegate.writeStartElement(arg0, capitalize(arg1));

And I use annotations in package-info for namespaces.我在命名空间的 package-info 中使用注释。

I tried to use marshaller.setAdapter() but nothing happends.我尝试使用marshaller.setAdapter()但什么也没发生。

I couldn't find a global way to remove the empty tag completely (other than by not setting the property in pojo before serialisation) however if you like to convert all empty strings to nulls ie <tagA></tagA> to <tagA/> you can use XmlAdapter class as following;我找不到完全删除空标记的全局方法(除了在序列化之前不在 pojo 中设置属性)但是,如果您想将所有空字符串转换为空值,即<tagA></tagA><tagA/>您可以使用XmlAdapter类,如下所示;

Step 1 Extend XmlAdapter步骤 1扩展XmlAdapter

package com.xyz.mylib;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class EmptyValueCleanerXmlAdapter extends XmlAdapter<String, String> {
    @Override
    public String unmarshal(String value) {
        return value;
    }

    @Override
    public String marshal(String value) {
        return (value != null && value.length() == 0) ? null : value;
    }
}

Step 2: Apply on pojo: There are you apply it different ways but one of the global way would be to create a package-info.java file as following (it would go into the package of whereever your pojo is)第 2 步:在 pojo 上应用:您可以通过不同的方式应用它,但其中一种全局方式是创建一个 package-info.java 文件,如下所示(它会进入您的 pojo 所在的包中)

@XmlJavaTypeAdapters({
    @XmlJavaTypeAdapter(EmptyValueCleanerXmlAdapter.class)
})
package com.myapp.pojos;

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;
import com.xyz.mylib.EmptyValueCleanerXmlAdapter;

Edit: Alternative solution编辑:替代解决方案
I end up using regex on xml to strip away any empty tags.我最终在 xml 上使用正则表达式来去除任何空标签。 see below见下文

// identifies empty tag i.e <tag1></tag> or <tag/>
// it also supports the possibilities of white spaces around or within the tag. however tags with whitespace as value will not match.
private static final String EMPTY_VALUED_TAG_REGEX = "\\s*<\\s*(\\w+)\\s*></\\s*\\1\\s*>|\\s*<\\s*\\w+\\s*/\\s*>";

Run the code on ideoneideone上运行代码

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

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