You could use the @XmlPath extension in EclipseLink JAXB (MOXy) to handle this use case. Note: I m the MOXy lead.
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;
}
<>Demo
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);
}
}
<>strong>jaxb.properties
• 利用MOXy作为你的日本宇宙航空研究开发机构提供商,需要在与你的模型班相同的一揽子方案中添加一个名为“jaxb.properties”的文件,其条目如下:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
http://www.ohchr.org。
<?xml version="1.0" encoding="UTF-8"?>
<notification>
<date>04/29/11</date>
<ccNum>3456</ccNum>
<expiry_month>November</expiry_month>
</notification>
<>更多信息>