非常感謝您的回答。它似乎適用於一個非常簡單的場景,但對於稍微複雜的場景我沒有成功。
我試圖模仿你在我自己的領域的代碼,只有我urialrshalling數據而不是marshalling。
適配器類:
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]
引進與@XMLPath在註釋字段時,但是我得到的問題SimpleName域類。
修改域類
@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。我一直在使用莫西2.1.1和2.2.0-M3具有相同的結果
我已經通過電子郵件通知您,我使用調試您的問題Java項目,你會介意運行它,看看你得到同樣的結果?希望你會得到相同的輸出,我們可以找出導致錯誤行爲的增量。 – 2010-10-21 13:41:00