Вы можете использовать расширение @ XmlPath в EclipseLink JAXB (MOXy) для обработки этого варианта использования. Примечание: я МОКСИ-лидер.
Уведомление
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Notification{
@XmlElement(name="date")
private String notifDate;
@XmlPath(".")
private CreditCardInfo ccInfo;
}
CreditCardInfo
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
@XmlAccessorType(XmlAccessType.FIELD)
public class CreditCardInfo{
private int ccNum;
@XmlElement(name="expiry_month")
private String expiryMonth;
}
Демо
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(Notification.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Notification notification = (Notification) unmarshaller.unmarshal(new File("input.xml"));
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(notification, System.out);
}
}
jaxb.properties
Чтобы использовать MOXy в качестве вашего JAXB-провайдера, вам нужно добавить файл с именем jaxb.properties в тот же пакет, что и классы вашей модели, со следующей записью:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Вход / выход
<?xml version="1.0" encoding="UTF-8"?>
<notification>
<date>04/29/11</date>
<ccNum>3456</ccNum>
<expiry_month>November</expiry_month>
</notification>
Для получения дополнительной информации