非常感谢您的回答。它似乎适用于一个非常简单的场景,但对于稍微复杂的场景我没有成功。
我试图在我自己的域中模仿你的代码,只是我在编组数据而不是编组。
适配器类:
public class DeleteStringAdapter extends XmlAdapter<DeletableString, String> {
@Override
public DeletableString marshal(String value) throws Exception {
System.out.println("MARSHALL: " + value);
return new DeletableString(value);
}
@Override
public String unmarshal(DeletableString v) throws Exception {
System.out.println("UNMARSHALL: " + v);
if(v.isDelete() != null && v.isDelete()){
return null;
}
return v.getValue();
}
}
适配器类型:
public class DeletableString {
public DeletableString() {
}
public DeletableString(String value) {
this.value = value;
}
private Boolean delete;
private String value;
@XmlAttribute
public Boolean isDelete() {
return delete;
}
public void setDelete(Boolean delete) {
this.delete = delete;
}
@XmlValue
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return DeletableString.class.getSimpleName() + "[delete=" + isDelete() + ", value=" + value + "]";
}
}
带注释的工作域类:
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlRootElement(name = "xml-fragment")
public class SimpleNavne implements Serializable{
private String forNavn = "";
private String fornavneMrkKode = "";
@XmlElement(name="PersonGivenName")
@XmlJavaTypeAdapter(value = DeleteStringAdapter.class)
public String getForNavn() {
return forNavn;
}
public void setForNavn(String forNavn) {
this.forNavn = forNavn;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(SimpleNavne.class.getSimpleName() + "[");
builder.append("forNavn=");
builder.append(forNavn);
builder.append("]");
return builder.toString();
}
}
演示
public class AppTest {
@Test
public void testApp() throws Exception {
System.setProperty("jaxb.debug", "true");
try{
JAXBContext jaxbContext = JAXBContext.newInstance(SimpleNavne.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Object ps = unmarshaller.unmarshal(new File("./personname-test4.xml"));
System.out.println(ps);
}catch(Exception e){
e.printStackTrace();
}
}
}
文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns:xml-fragment xmlns:ns="http://cpr.csc.com/navne">
<ns:PersonGivenName delete="false">010</ns:PersonGivenName>
</ns:xml-fragment>
如果从输出中得出结论,以上似乎工作正常
SimpleNavne[forNavn=010]
但是,在 SimpleName 域类中引入使用 @XMLPath 注释的字段时遇到问题。
修改域类
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlRootElement(name = "xml-fragment")
public class SimpleNavne implements Serializable{
private String forNavn = "";
private String fornavneMrkKode = "";
@XmlElement(name="PersonGivenName")
@XmlJavaTypeAdapter(value = DeleteStringAdapter.class)
public String getForNavn() {
return forNavn;
}
public void setForNavn(String forNavn) {
this.forNavn = forNavn;
}
@XmlPath("/PersonGivenNameMarkingStructure/MarkingCode/text()")
public String getFornavneMrkKode() {
return fornavneMrkKode;
}
public void setFornavneMrkKode(String forNavnMrk) {
this.fornavneMrkKode = forNavnMrk;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(SimpleNavne.class.getSimpleName() + "[");
builder.append(", forNavn=");
builder.append(forNavn);
builder.append(", fornavneMrkKode=");
builder.append(fornavneMrkKode);
builder.append("]");
return builder.toString();
}
}
修改文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns:xml-fragment xmlns:ns="http://cpr.csc.com/navne">
<ns:PersonGivenName delete="true">010</ns:PersonGivenName>
<ns:PersonGivenNameMarkingStructure>
<ns:MarkingCode>011</ns:MarkingCode>
</ns:PersonGivenNameMarkingStructure>
</ns:xml-fragment>
输出是:
SimpleNavne[forNavn=010, fornavneMrkKode=]
但应该是:
SimpleNavne[forNavn=010, fornavneMrkKode=011]
我是在做完全错误的事情还是 MOXy 不支持这种情况?
PS。我曾尝试使用 MOXy 2.1.1 和 2.2.0-M3,结果相同