コレクション/配列プロパティがシリアル化されて次のようなものを取得するたびに、処理命令を追加したいと思います
<alice>
<? array bob ?>
<bob>edgar</bob>
<bob>david</bob>
</alice>
これはJAXBで可能ですか?または、少なくとも特定の JAXB 実装では?
コレクション/配列プロパティがシリアル化されて次のようなものを取得するたびに、処理命令を追加したいと思います
<alice>
<? array bob ?>
<bob>edgar</bob>
<bob>david</bob>
</alice>
これはJAXBで可能ですか?または、少なくとも特定の JAXB 実装では?
これを行うには、XMLStreamWriter
と を利用できます。XmlAdapter
ボブアダプター
に関する注意事項XmlAdapter
:
XMLStreamWriter
ます。XmlAdapter
後ほど、 にステートフルを設定する JAXB の機能を活用しますMarshaller
。List<String>
しList<String>
ているだけです。XmlAdapter
marshal
メソッドは、次のように呼び出す場所writeProcessingInstruction
ですXMLStreamWriter
。
package forum6931520;
import java.util.List;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.stream.XMLStreamWriter;
public class BobAdapter extends XmlAdapter<List<String>, List<String>> {
private boolean first = true;
private XMLStreamWriter xmlStreamWriter;
public BobAdapter() {
}
public BobAdapter(XMLStreamWriter xmlStreamWriter) {
this();
this.xmlStreamWriter = xmlStreamWriter;
}
@Override
public List<String> marshal(List<String> stringList) throws Exception {
if(first) {
xmlStreamWriter.writeProcessingInstruction("array", "bob");
first = false;
}
return stringList;
}
@Override
public List<String> unmarshal(List<String> stringList) throws Exception {
return stringList;
}
}
アリス
注釈は、フィールド/プロパティと@XmlJavaTypeAdapter
リンクするために使用されます。XmlAdapter
package forum6931520;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlRootElement
public class Alice {
private List<String> bob;
@XmlJavaTypeAdapter(BobAdapter.class)
public List<String> getBob() {
return bob;
}
public void setBob(List<String> bob) {
this.bob = bob;
}
}
デモ
package forum6931520;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Alice.class);
File xml = new File("src/forum6931520/input.xml");
Unmarshaller unmarshaller = jc.createUnmarshaller();
Alice alice = (Alice) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
XMLOutputFactory xof = XMLOutputFactory.newFactory();
XMLStreamWriter xmlStreamWriter = xof.createXMLStreamWriter(System.out);
marshaller.setAdapter(new BobAdapter(xmlStreamWriter));
marshaller.marshal(alice, xmlStreamWriter);
}
}
入力.xml
<?xml version="1.0" encoding="UTF-8"?>
<alice>
<?array bob?>
<bob>edgar</bob>
<bob>david</bob>
</alice>
出力
<?xml version='1.0' encoding='UTF-8'?><alice><?array bob?><bob>edgar</bob><bob>david</bob></alice>
ノート
この例は、 EclipseLink JAXB (MOXy)で実行する必要があります。これは、JAXB RI が次の例外をスローするためです (私は MOXy リーダーです)。
Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
java.util.List is an interface, and JAXB can't handle interfaces.
this problem is related to the following location:
at java.util.List
at public java.util.List forum6931520.Alice.getBob()
at forum6931520.Alice
java.util.List does not have a no-arg default constructor.
this problem is related to the following location:
at java.util.List
at public java.util.List forum6931520.Alice.getBob()
at forum6931520.Alice
詳細については
アップデート
命令を処理するためのネイティブ サポートを追加するための拡張要求 ( https://bugs.eclipse.org/354286 ) を入力しました。これにより、最終的にはプロパティで次を指定できるようになります。
@XmlProcessingInstruction(target="array", value="bob")
public List<String> getBob() {
return bob;
}