我正在尝试将 POJO 映射到 xml。现在 POJO 由一些基本的 String/Integer 等属性和一些其他 POJO 类组成。我尝试创建的 xml 映射文件中,我希望顶层的 xml 元素填充有 2 层深度的 java 属性。
例如,如果我有 2 个 java 类 `
class classA{
private ClassB var1
private String var2
}
class classB{
private ClassC var3;
}
class classC{
private String var4;
}
现在我的 xml 映射看起来像
邮轮 map
<class name="ClassA"
auto-complete="false"
>
<map-to xml="Sample" />
<field name="var2">
<bind-xml node="attribute" name="var2" />
</field>
<field name="var4 from classC">
<I want a mapping for the var4 from classC to appear here. How do I do that ?>
</class>
` 如您所见,我想要 xml 元素示例中的 classC 的 var4 映射。我希望它成为示例元素的一个元素。
`
<Sample var2="value">
<data var4="var4 value">
</Sample>
`
感谢帮助!
最佳答案
注意:我是EclipseLink JAXB (MOXy) JAXB 2 (JSR-222) 的领导者和成员专家组。
Blaise : I have not used EclipseLink before, however I think I can move out of castor if it is relatively simple to adopt EclipseLink ? Its just that the entire project has been using Castor so it would be more consistent. However, could you please elaborate on Eclipselink. Thanks. – TYS
EclipseLink MOXy 是一种 JAXB (JSR-222) 实现。查看您的问题,可以使用任何 JAXB 实现将该模型映射到所需的 XML,如下所示:
A类
package forum9994762;
import javax.xml.bind.annotation.*;
@XmlRootElement(name="Sample")
@XmlAccessorType(XmlAccessType.FIELD)
public class ClassA {
@XmlElement(name="data")
private ClassB var1;
@XmlAttribute
private String var2;
}
B类
package forum9994762;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class ClassB {
@XmlAttribute(name="var4")
private ClassC var3;
}
C类
package forum9994762;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class ClassC {
@XmlValue
private String var4;
}
演示
package forum9994762;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(ClassA.class);
File xml = new File("src/forum9994762/input.xml");
Unmarshaller unmarshaller = jc.createUnmarshaller();
ClassA classA = (ClassA) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(classA, System.out);
}
}
输入.xml/输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Sample var2="value">
<data var4="var4 value"/>
</Sample>
<小时/>
映射文件
作为 Castor 用户,您可能更喜欢将元数据表示为外部映射文件。 EclipseLink MOXy 提供了这样的扩展:
绑定(bind).xml
<?xml version="1.0" encoding="UTF-8"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum9994762"
xml-accessor-type="FIELD">
<java-types>
<java-type name="ClassA">
<xml-root-element name="Sample"/>
<java-attributes>
<xml-element java-attribute="var1" name="data"/>
<xml-attribute java-attribute="var2"/>
</java-attributes>
</java-type>
<java-type name="ClassB">
<java-attributes>
<xml-attribute java-attribute="var3" name="var4"/>
</java-attributes>
</java-type>
<java-type name="ClassC">
<java-attributes>
<xml-value java-attribute="var4"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
演示
package forum9994762;
import java.io.File;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>(1);
properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum9994762/binding.xml");
JAXBContext jc = JAXBContext.newInstance("forum9994762", ClassA.class.getClassLoader(), properties);
File xml = new File("src/forum9994762/input.xml");
Unmarshaller unmarshaller = jc.createUnmarshaller();
ClassA classA = (ClassA) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(classA, System.out);
}
}
了解更多信息
关于Java 到 XML 的 Castor 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9994762/