【问题标题】:JaxB to read class hierarchyJaxB 读取类层次结构
【发布时间】:2013-04-01 09:56:29
【问题描述】:

只是扩展Parsing class hierarchy using JaxB 问题。 想要使用 JaxB 读取以下 xml 文件

<IMPORT>
    <TABLE NAME="USER">
        <ROW>
            <USER_ID>1</USER_ID>
            <ROW_VERSION>1</ROW_VERSION>
            <USER_NAME>Navnath</USER_NAME>
            <LOGIN>Navnath</LOGIN>
            <LOGIN_PASSWORD>Navnath</LOGIN_PASSWORD>
        </ROW>
        <ROW>
            <USER_ID>2</USER_ID>
            <ROW_VERSION>1</ROW_VERSION>
            <USER_NAME>Kumbhar</USER_NAME>
            <LOGIN>Kumbhar</LOGIN>
            <LOGIN_PASSWORD>Kumbhar</LOGIN_PASSWORD>
        </ROW>
    </TABLE>

    <TABLE NAME="WORK">
        <ROW>
            <WORK_ID>1</WORK_ID>
            <WORK_NAME>Work1</WORK_NAME>
            <ROW_VERSION TYPE="N">1</ROW_VERSION>
        </ROW>
        <ROW>
            <WORK_ID>2</WORK_ID>
            <WORK_NAME>Work2</WORK_NAME>
            <ROW_VERSION TYPE="N">1</ROW_VERSION>
        </ROW>
    </TABLE>
    <TABLE> ... </TABLE>
    <TABLE> ... </TABLE>
    <TABLE> ... </TABLE>
</IMPORT>

您可以在上面的 xml 文件中看到每个表中的列名不同。我想将此数据插入数据库。我尝试为此创建类层次结构,但我不知道该怎么做。我的 ROW 类将包含每个表的不同 xml 元素,这是我无法配置的区域。请提出建议。

【问题讨论】:

  • 您正在寻找基于“NAME”属性的特定实现类,以确定用于加载 ROW 数据的类?

标签: java xml-parsing jaxb


【解决方案1】:

注意:我是EclipseLink JAXB (MOXy) 领导,也是JAXB (JSR-222) 专家组的成员。

对于这个用例,您可以利用 MOXy 的 @XmlDescriminatorNode/@XmlDescriminatorValue 扩展(请参阅:http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-moxy-extension.html)。

导入

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlRootElement(name="IMPORT")
@XmlAccessorType(XmlAccessType.FIELD)
public class Import {

    @XmlElement(name="TABLE")
    private List<Table> tables;

}

表格

@XmlDescriminatorNode 注释用于指定 XML 属性,该属性将用于指示将实例化哪个子类。 JAXB 实现不能通过反射引入类的子类,我们将使用@XmlSeeAlso 注释来引用它们。

import javax.xml.bind.annotation.XmlSeeAlso;
import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorNode;

@XmlDiscriminatorNode("@NAME")
@XmlSeeAlso({UserTable.class, WorkTable.class})
public abstract class Table {

}

用户表

@XmlDescriminatorValue 注解用于指定对应于特定子类的NAME 属性的值。

import java.util.List;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorValue;

@XmlDiscriminatorValue("USER")
@XmlAccessorType(XmlAccessType.FIELD)
public class UserTable extends Table {

    @XmlElement(name="ROW")
    private List<UserRow> rows;

}

用户行

import javax.xml.bind.annotation.XmlElement;

public class UserRow {

    @XmlElement(name="USER_ID")
    private int userID;

    @XmlElement(name="USER_NAME")
    private String userName;

}

工作表

import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorValue;

@XmlDiscriminatorValue("WORK")
public class WorkTable extends Table {

}

jaxb.properties

要将 MOXy 指定为您的 JAXB 提供程序,您需要在与域模型相同的包中包含一个名为 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(Import.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum15741264/input.xml");
        Import result = (Import) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(result, System.out);
    }

}

input.xml/Output

以下是运行演示代码的输入和输出。

<?xml version="1.0" encoding="UTF-8"?>
<IMPORT>
   <TABLE NAME="USER">
      <ROW>
         <USER_ID>1</USER_ID>
         <USER_NAME>Navnath</USER_NAME>
      </ROW>
      <ROW>
         <USER_ID>2</USER_ID>
         <USER_NAME>Kumbhar</USER_NAME>
      </ROW>
   </TABLE>
   <TABLE NAME="WORK"/>
</IMPORT>

替代解决方案

或者仅使用标准 JAXB API,您可以使用 XmlAdapter 尝试以下方法

【讨论】:

  • 我尝试了这段代码但得到了异常Exception [EclipseLink-3002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.ConversionException Exception Description: The object [], of class [class java.lang.String], from mapping [org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping[ROW]] with descriptor [XMLDescriptor(org.jaxb.UserTable --&gt; [])], could not be converted to [class org.jaxb.UserRow]. at org.eclipse.persistence.exceptions.ConversionException.couldNotBeConverted(ConversionException.java:71)
  • @Navnath - 你能用 EclipseLink 2.4.1 试试代码吗:eclipse.org/eclipselink/downloads。另外,如果您通过我的联系包给我发信息,我会将代码通过电子邮件发送给您:blog.bdoughan.com/p/contact_01.html
猜你喜欢
  • 2013-03-18
  • 2011-06-07
  • 1970-01-01
  • 1970-01-01
  • 2011-05-19
  • 1970-01-01
  • 1970-01-01
  • 2013-09-17
  • 2017-01-24
相关资源
最近更新 更多