gpt4 book ai didi

java - JAXB 编码,忽略 nillable

转载 作者:数据小太阳 更新时间:2023-10-29 02:10:03 24 4
gpt4 key购买 nike

我有一个带有注释 nillable=true 的变量的类,我不希望它们出现在 xml 中。该类是从无法更改的 xsd 生成的。

例子:一个看起来像这样的类:

public class Hi {
...
@XmlElement(name = "hello", nillable = true)
protected Long hello;
...
}

对象被 JAXBContext 创建的编码器编码。生成的xml变成:

...
<hello xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
...

Hi 类是从无法更改的 xsd 生成的。我的问题是,如果“hello”为空,是否有办法让编码器忽略可空参数并且不向 xml 输出任何内容?

最佳答案

一种方法是实现 decorator XMLStreamWriter 类型并在其中实现您的过滤器。

这是一个非常基本和幼稚的(它没有涵盖很多东西,比如命名空间和许多其他东西),它可以适用于你的情况,但它并不意味着完美,它只是为了展示想法:

public class FilteredXMLStreamWriter implements XMLStreamWriter {
private final XMLStreamWriter writer;
private final Set<String> pathsToSkip;
private final Stack<String> path = new Stack<>();
private boolean ignore;

public FilteredXMLStreamWriter(XMLStreamWriter writer, Set<String> pathsToSkip) {
this.writer = writer;
this.pathsToSkip = pathsToSkip;
}

/**
* Build the current path from the Stack
*/
private String toPath() {
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String element : path) {
if (first) {
first = false;
} else {
sb.append('/');
}
sb.append(element);
}
return sb.toString();
}

public void writeStartElement(String prefix, String localName, String namespaceURI)
throws XMLStreamException {
// Add the current
path.push(localName);
if (!ignore) {
this.ignore = pathsToSkip.contains(toPath());
if (!ignore) {
this.writer.writeStartElement(prefix, localName, namespaceURI);
}
}
}
...

public void writeEndElement() throws XMLStreamException {
if (ignore) {
this.ignore = !pathsToSkip.contains(toPath());
} else {
this.writer.writeEndElement();
}
path.pop();
}
...

public void writeCharacters(String text) throws XMLStreamException {
if (!ignore) {
this.writer.writeCharacters(text);
}
}

public void writeCharacters(char[] text, int start, int len)
throws XMLStreamException {
if (!ignore) {
this.writer.writeCharacters(text, start, len);
}
}

...
}

这是一种让路径跳过的简单方法:

private static Set<String> pathsToSkip(Class<?> clazz) {
// Make sure that the class is annotated with XmlRootElement
XmlRootElement rootElement = clazz.getAnnotation(XmlRootElement.class);
if (rootElement == null) {
throw new IllegalArgumentException("XmlRootElement is missing");
}
// Create the root name from the annotation or from the class name
String rootName = ("##default".equals(rootElement.name()) ?
clazz.getSimpleName().substring(0, 1).toLowerCase() + clazz.getSimpleName().substring(1) :
rootElement.name());
// Set that will contain all the paths
Set<String> pathsToSkip = new HashSet<>();
addPathsToSkip(rootName, clazz, pathsToSkip);
return pathsToSkip;
}

private static void addPathsToSkip(String parentPath, Class<?> clazz,
Set<String> pathsToSkip) {
// Iterate over all the fields
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
XmlElement xmlElement = field.getAnnotation(XmlElement.class);
if (xmlElement != null) {
// Create the name of the element from the annotation or the field name
String elementName = ("##default".equals(xmlElement.name()) ?
field.getName() :
xmlElement.name());
String path = parentPath + "/" + elementName;
if (xmlElement.nillable()) {
// It is nillable so we add it to the paths to skip
pathsToSkip.add(path);
} else {
// It is not nillable so we check the fields corresponding
// to the field type
addPathsToSkip(path, field.getType(), pathsToSkip);
}
}
}
}

然后这里是你将如何调用它:

marshaller.marshal(
myObject,
new FilteredXMLStreamWriter(
XMLOutputFactory.newInstance().createXMLStreamWriter(sw),
pathsToSkip(Hi.class)
)
);

关于java - JAXB 编码,忽略 nillable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39204125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com