问题描述
只需使用JaxB扩展问题。
想要使用JaxB阅读以下xml文件
Just extending Parsing class hierarchy using JaxB question.Want to read following xml file using JaxB
<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元素,这是我无法配置的区域。请建议。
You can see in above xml file is column names are different in each table. I want to insert this data in database. I try to create class hierarchy for this, But I don't know how to do this. My ROW class will contains diffrent xml element per table and this is the area which I am not able to configure. Please suggest.
推荐答案
注意:我是领导和专家组。
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
您可以利用MOXy的 @XmlDescriminatorNode
/ @XmlDescriminatorValue
扩展此用例(请参阅:)。
You could leverage MOXy's @XmlDescriminatorNode
/@XmlDescriminatorValue
extension for this use case (see: 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
注释来引用它们。
The @XmlDescriminatorNode
annotation is used to specify the XML attribute that will be used to indicate which subclass will be instantiated. A JAXB implementation can't pull in the subclasses of a class via reflection, we will use the @XmlSeeAlso
annotation to reference them.
import javax.xml.bind.annotation.XmlSeeAlso;
import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorNode;
@XmlDiscriminatorNode("@NAME")
@XmlSeeAlso({UserTable.class, WorkTable.class})
public abstract class Table {
}
UserTable
@XmlDescriminatorValue
注释用于指定与特定子类对应的 NAME
属性的值。
The @XmlDescriminatorValue
annotation is used to specify the value of the NAME
attribute that corresponds to a particular subclass.
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;
}
UserRow
import javax.xml.bind.annotation.XmlElement;
public class UserRow {
@XmlElement(name="USER_ID")
private int userID;
@XmlElement(name="USER_NAME")
private String userName;
}
WorkTable
import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorValue;
@XmlDiscriminatorValue("WORK")
public class WorkTable extends Table {
}
jaxb.properties
要将MOXy指定为JAXB提供程序,您需要包含一个名为 jaxb.properties
的文件与您的域模型在同一个包中,并带有以下条目(请参阅:) 。
To specify MOXy as your JAXB provider you need to include a file called jaxb.properties
in the same package as your domain model with the following entry (see: 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 /输出
以下是运行演示代码的输入和输出。
Below is the input to and output from running the demo code.
<?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
Alternatively using only the standard JAXB APIs you could try the following approach using an XmlAdapter
- $ b $尝试以下方法b
- http://blog.bdoughan.com/2012/01/jaxb-and-inhertiance-using-xmladapter.html
这篇关于JaxB读取类层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!