gpt4 book ai didi

java - JAXB/MOXy : Do not call XmlElementWrapper setter when element missing?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:56:31 25 4
gpt4 key购买 nike

我在类上有一个列表 setter ,该类同时使用@XmlElementWrapper(name = "foos") 和@XmlElement(name = "foo") 进行注释。

当我解码没有 元素的 XML 时,将调用 setter 并传递一个空列表。有没有办法获得以下内容?:

  • 没有 时,不调用setter。或者,如果必须调用 setter,则传递 null。
  • 存在但为空时,将一个空列表传递给 setter 。
  • 有一个或多个子 元素时,传递一个填充列表。

最佳答案

对于这个用例,您可以使用 XmlAdapter:

input1.xml

When there is no , do not call the setter. Or if the setter must be called, pass null.

<?xml version="1.0" encoding="UTF-8"?>
<root>
<child/>
</root>

input2.xml

When is present but empty, pass an empty list to the setter.

<?xml version="1.0" encoding="UTF-8"?>
<root>
<child>
<foos/>
</child>
</root>

input3.xml

When has one or more child elements, pass a populated list.

<?xml version="1.0" encoding="UTF-8"?>
<root>
<child>
<foos>
<foo>Hello World</foo>
</foos>
</child>
</root>

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
public class Root {

private Child child;

@XmlJavaTypeAdapter(ChildAdapter.class)
public Child getChild() {
return child;
}

public void setChild(Child child) {
this.child = child;
}

}

child

import java.util.List;

public class Child {

private List<String> strings;

public List<String> getStrings() {
return strings;
}

public void setStrings(List<String> strings) {
System.out.println("setStrings");
this.strings = strings;
}

}

子适配器

import java.util.ArrayList;
import java.util.List;

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

public class ChildAdapter extends XmlAdapter<ChildAdapter.AdaptedChild, Child> {

public static class AdaptedChild {
public Foos foos;
}

public static class Foos {
public List<String> foo;
}

@Override
public Child unmarshal(AdaptedChild adaptedChild) throws Exception {
Child child = new Child();
Foos foos = adaptedChild.foos;
if(null != foos) {
List<String> foo = foos.foo;
if(null == foo) {
child.setStrings(new ArrayList<String>());
} else {
child.setStrings(foos.foo);
}
}
return child;
}

@Override
public AdaptedChild marshal(Child child) throws Exception {
AdaptedChild adaptedChild = new AdaptedChild();
List<String> strings = child.getStrings();
if(null != strings) {
Foos foos = new Foos();
foos.foo = strings;
adaptedChild.foos = foos;
}
return adaptedChild;
}

}

演示

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {

public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);

Unmarshaller unmarshaller = jc.createUnmarshaller();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Object o;

o = unmarshaller.unmarshal(new File("input1.xml"));
marshaller.marshal(o, System.out);

o = unmarshaller.unmarshal(new File("input2.xml"));
marshaller.marshal(o, System.out);

o = unmarshaller.unmarshal(new File("input3.xml"));
marshaller.marshal(o, System.out);
}

}

输出

<?xml version="1.0" encoding="UTF-8"?>
<root>
<child/>
</root>
setStrings
<?xml version="1.0" encoding="UTF-8"?>
<root>
<child>
<foos/>
</child>
</root>
setStrings
<?xml version="1.0" encoding="UTF-8"?>
<root>
<child>
<foos>
<foo>Hello World</foo>
</foos>
</child>
</root>

关于java - JAXB/MOXy : Do not call XmlElementWrapper setter when element missing?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6065066/

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