注: 私はEclipseLink JAXB (MOXy)のリーダーであり、JAXB (JSR-222)エキスパート グループのメンバーです。
デッキー
以下は、MOXy の拡張機能を使用してユース ケースをマッピングする方法です@XmlPath
(参照: http://blog.bdoughan.com/2010/07/xpath-based-mapping.html )。
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement(name="Dekkey")
@XmlAccessorType(XmlAccessType.FIELD)
public class Dekkey {
@XmlAttribute
String keyVal;
@XmlPath("kek1/@keyVal")
String kek1;
@XmlPath("kek1/userkey/@keyVal")
String userKey;
}
jaxb.properties
MOXy を JAXB (JSR-222) プロバイダーとして指定するにはjaxb.properties
、次のエントリを使用して、ドメイン モデルと同じパッケージに呼び出されるファイルを含める必要があります (参照: http://blog.bdoughan.com/2011/05/specifying- eclipselink-moxy-as-your.html )。
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
デモ
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Dekkey.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum16248263/input.xml");
Dekkey dekkey = (Dekkey) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(dekkey, System.out);
}
}
input.xml/出力
<?xml version="1.0" encoding="UTF-8"?>
<Dekkey keyVal="xer">
<kek1 keyVal="biv">
<userkey keyVal="wed"/>
</kek1>
</Dekkey>