JaxB to read class hierarchy

落爺英雄遲暮 提交于 2019-12-11 01:17:29

问题


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>

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.


回答1:


Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

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

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;

}

Table

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

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

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

Demo

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

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>

Alternative Solutions

Alternatively using only the standard JAXB APIs you could try the following approach using an XmlAdapter

  • http://blog.bdoughan.com/2012/01/jaxb-and-inhertiance-using-xmladapter.html


来源:https://stackoverflow.com/questions/15741264/jaxb-to-read-class-hierarchy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!