Java到XML的castor映射



我正在尝试将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映射看起来像

CruiseLine映射

<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元素Sample中classC中var4的映射。我希望它是示例元素的一个元素。

`

<Sample var2="value">
     <data var4="var4 value">
</Sample>

`

感谢帮助!

注意:我是EclipseLink JAXB(MOXy)的负责人,也是JAXB 2(JSR-222)

Blaise:我以前没有使用过EclipseLink,但我想我可以如果采用EclipseLink相对简单,请退出castor?只是整个项目都在使用Castor,所以更加一致。不过,你能详细介绍一下Eclipselink吗。谢谢TYS

EclipseLinkMOXy是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;
}

ClassB

package forum9994762;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class ClassB {
    @XmlAttribute(name="var4")
    private ClassC var3;
}

ClassC

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);
    }
}

input.xml/Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Sample var2="value">
    <data var4="var4 value"/>
</Sample>

映射文件

作为Castor用户,您可能更喜欢将元数据表示为外部映射文件。EclipseLink MOXy提供这样一个扩展:

binding.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);
    }
}

有关更多信息

  • http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html
  • http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html

如果在ClassA <class name="ClassA" auto-complete="false" >的mapping.xml文件中使用auto-complete=false,则必须告诉Castor marshaller如何分别映射ClassB和ClassC。包括这两行可能有助于您入门:

<class name="ClassB" auto-complete="true">
 <map-to xml="class-B" />
</class>
<class name="ClassC" auto-complete="true">
 <map-to xml="class-C" />
</class>

但是由于var4是ClassC的成员,而不是ClassB的成员,所以它将是xml输出层次结构中的一个层次。

相关内容

  • 没有找到相关文章

最新更新