【问题标题】:Unmarshalling Dozer mapping files to provide a mapping library解组 Dozer 映射文件以提供映射库
【发布时间】:2012-06-26 23:32:48
【问题描述】:

我正在尝试解组一些推土机映射文件,以便为许多应用程序提供映射可用性库。但我无法让 JaxB 注释正常工作。我们将映射列表解组为 null 或空

从映射文件中,我感兴趣的是。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<mappings>
    <mapping>
        <class-a>package.MySourceClass</class-a>
        <class-b>other.package.DestinationClass</class-b>
    </mapping>
</mappings>

我有一个映射类

@XmlRootElement(name="mappings")
@XmlAccessorType(XmlAccessType.FIELD)
public class Mappings {

    @XmlElementWrapper(name="mappings")
    private List<Mapping> mappingEntries = null;

//Getters and setters omitted

和一个映射类

@XmlRootElement(name="mapping")
@XmlAccessorType(XmlAccessType.FIELD)
public class Mapping {


    @XmlElement(name ="class-a")
    private String classA;

    @XmlElement(name = "class-b")
    private String classB;

我尝试了许多注释组合,但我无法弄清楚我做错了什么。

谁能指出我正确的方向。

【问题讨论】:

    标签: java xml jaxb mapping dozer


    【解决方案1】:

    您可以执行以下操作:

    映射

    package forum11193953;
    
    import java.util.List;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement(name="mappings") // Match the root element "mappings"
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Mappings {
    
        @XmlElement(name="mapping") // There will be a "mapping" element for each item.
        private List<Mapping> mappingEntries = null;
    
    }
    

    映射

    package forum11193953;
    
    import javax.xml.bind.annotation.*;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Mapping {
    
    
        @XmlElement(name ="class-a")
        private String classA;
    
        @XmlElement(name = "class-b")
        private String classB;
    
    }
    

    演示

    package forum11193953;
    
    import java.io.File;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Mappings.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml= new File("src/forum11193953/input.xml");
            Mappings mappings = (Mappings) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(mappings, System.out);
        }
    
    }
    

    input.xml/Output

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <mappings>
        <mapping>
            <class-a>package.MySourceClass</class-a>
            <class-b>other.package.DestinationClass</class-b>
        </mapping>
    </mappings>
    

    【讨论】:

    • 在该设置中,列表被实例化但包含 0 个元素
    • 我已经扩展了我的答案以包含一个完整的示例,但我们的初始映射是正确的。您的模型可能有问题。
    • 先生,您是摇滚明星,非常感谢。我从未在映射列表中将 XmlElementWrapper 更改为 XmlElement。
    【解决方案2】:

    试用 JMapper 框架:http://code.google.com/p/jmapper-framework/

    使用 JMapper,您可以拥有动态映射的所有优势以及静态代码的性能等等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-19
      相关资源
      最近更新 更多