您的映射看起来正确,下面是一个完整的示例。由于您正在注释该字段,因此请确保您@XmlAccessorType(XmlAccessType.FIELD)
在课堂上拥有(参见:http ://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html )。
领域模型(根)
import java.util.List;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
@XmlPath("jaxbBean/@file")
List<String> jaxbBeansClasses;
}
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(Root.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum17104179/input.xml");
Root root = (Root) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}
输入.xml/输出
<?xml version="1.0" encoding="UTF-8"?>
<root>
<jaxbBean file="A.groovy"/>
<jaxbBean file="B.groovy"/>
</root>