【问题标题】:How to map two XML element levels to one level on XStream or JAXB?如何将两个 XML 元素级别映射到 XStream 或 JAXB 上的一个级别?
【发布时间】:2017-02-24 15:56:53
【问题描述】:

在我的 Java Bean 中将两级 XML 元素映射到一级时遇到了一些困难。这是我的上下文,我有一个这样的 XML:

<?xml version='1.0' encoding="utf-8" ?>
<Employee>
    <Data>
        <CompanyId>1</CompanyId>
        <FirstName>John</FirstName>
        <LastName>Oliver</LastName>
        <DOB>1986-21-07</DOB>
    </Data>
</Employee>

这是我的 Java Bean:

@XStreamAlias("Employee/Data")
public  class Employee {
    @XStreamAlias("CompanyId") private int companyId;
    @XStreamAlias("FirstName") private String firstName;
    @XStreamAlias("LastName")  private String lastName;
    @XStreamAlias("DOB")       private LocalDate birthDate;
    public int getCompanyId() { return companyId; }
    public void setCompanyId(int companyId) { this.companyId = companyId; }
    public String getFirstName() { return firstName; }
    public void setFirstName(String firstName) { this.firstName = firstName; }
    public String getLastName() { return lastName; }
    public void setLastName(String lastName) { this.lastName = lastName; }
    public LocalDate getBirthDate() { return birthDate; }
    public void setBirthDate(LocalDate birthDate) { this.birthDate = birthDate; }
}

我把 XML 元素用“/”隔开只是为了说明我想如何映射,但 XStream 似乎不适用于这种方式。使用注释映射的任何技巧还是我应该编写自定义转换器?如果有人知道如何在 JAXB 中进行此映射,也欢迎。

【问题讨论】:

    标签: java xml jaxb mapping xstream


    【解决方案1】:

    我找到了一个使用 JAXB + eclipselink moxy 的解决方案。首先添加 eclipselink 依赖 'org.eclipse.persistence:org.eclipse.persistence.moxy:2.6.4' 并确保您在应用程序中使用 eclipselink 实现,您需要通过命令行传递 JVM 参数

    -Djavax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

    或者像我一样在你的方法 main 中设置:

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.InputStream;
    import java.io.PrintWriter;
    import java.io.StringWriter;
    import java.time.LocalDate;
    import java.time.Month;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlRootElement;
    
    import org.eclipse.persistence.oxm.annotations.XmlPath;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement(name = "Employee")
    public class Employee {
    
        @XmlPath("Data/CompanyId/text()") private int companyId;
        @XmlPath("Data/FirstName/text()") private String firstName;
        @XmlPath("Data/LastName/text()")  private String lastName;
        @XmlPath("Data/DOB/text()")       private LocalDate birthDate;
    
        public int getCompanyId() { return companyId; }
        public void setCompanyId(int companyId) { this.companyId = companyId; }
        public String getFirstName() { return firstName; }
        public void setFirstName(String firstName) { this.firstName = firstName; }
        public String getLastName() { return lastName; }
        public void setLastName(String lastName) { this.lastName = lastName; }
        public LocalDate getBirthDate() { return birthDate; }
        public void setBirthDate(LocalDate birthDate) { this.birthDate = birthDate; }
    
        public String toString() {
            return String.format("{companyId: %d, firstName: \"%s\", lastName: \"%s\"}", companyId, firstName, lastName);
        }
    
    
        public static void main(String[] args) throws Exception {
    
            System.setProperty("javax.xml.bind.context.factory", "org.eclipse.persistence.jaxb.JAXBContextFactory");
    
            Employee employee = new Employee();
            employee.setCompanyId(100);
            employee.setFirstName("Michael");
            employee.setLastName("Hoffmann");
            employee.setBirthDate(LocalDate.of(1970,  Month.JANUARY, 19));
    
            JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
            StringWriter sw = new StringWriter();
            PrintWriter out = new PrintWriter(sw);
    
            jaxbContext.createMarshaller().marshal(employee, out);
            out.flush();
            String xml = sw.toString();
            System.out.println(xml);
    
            InputStream in = new ByteArrayInputStream(xml.getBytes());
    
    
            Employee employee2 = (Employee) jaxbContext.createUnmarshaller().unmarshal(in);
            System.out.println(employee2);
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-12
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多