运行时的Jaxb配置

时间:2014-12-10 19:22:32

标签: jaxb

我有一个商业案例,我将有大约10种不同的pojo可用于映射到10个不同的传入xml文件,即。(位置,产品,订单,项目....)我想知道它们是否是JAXB中的一种方式配置它。可以是将xml文件映射到正确类的配置文件。因此,我不必在我的代码中输入逻辑来确定正确的类

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以执行类似于我在下面显示的粗略示例,使用文件将类映射到XML文件。您必须对其进行调整以满足您的特定需求。

我创建了一些非常简单的类来保存一些原始数据类型来说明用法:

<强> ClassOne:

package so27409179;

@XmlRootElement(name="ClassOne")
@XmlAccessorType(XmlAccessType.FIELD)
public class ClassOne {
    @XmlElement(name="Field")
    String field;

    public ClassOne() {

    }

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }

    @Override
    public String toString() {
        return "ClassOne\n" + "\tField = " + getField();
    }
}

<强> ClassTwo:

package so27409179;

@XmlRootElement(name="ClassTwo")
@XmlAccessorType(XmlAccessType.FIELD)
public class ClassTwo {
    @XmlElement(name="Field1")
    String field1;
    @XmlElement(name="Field2")
    int field2;
    @XmlElement(name="Field3")
    double field3;

    public ClassTwo() {

    }

    public String getField1() {
        return field1;
    }

    public void setField1(String field1) {
        this.field1 = field1;
    }

    public int getField2() {
        return field2;
    }

    public void setField2(int field2) {
        this.field2 = field2;
    }

    public double getField3() {
        return field3;
    }

    public void setField3(double field3) {
        this.field3 = field3;
    }

    @Override
    public String toString() {
        return "ClassTwo\n" +
                "\tField1 = " + getField1() +
                "\n\tField2 = " + getField2() +
                "\n\tField3 = " + getField3();
    }
}

<强> ClassThree:

package so27409179;

@XmlRootElement(name = "ClassThree")
@XmlAccessorType(XmlAccessType.FIELD)
public class ClassThree {
    @XmlElement(name="Field1")
    String field1;
    @XmlElement(name="Field2")
    String field2;

    public ClassThree() {

    }

    public String getField1() {
        return field1;
    }

    public void setField1(String field1) {
        this.field1 = field1;
    }

    public String getField2() {
        return field2;
    }

    public void setField2(String field2) {
        this.field2 = field2;
    }

    @Override
    public String toString() {
        return "ClassThree\n" +
                "\tField1 = " + getField1() +
                "\n\tField2 = " + getField2();
    }
}

然后我创建了一个简单的映射文件(以逗号分隔),将文件名映射到类名。我将此文件称为mappings.txt

XmlFile1.xml,so27409179.ClassOne
XmlFile2.xml,so27409179.ClassTwo
XmlFile3.xml,so27409179.ClassThree

然后我创建了一些虚拟XML文件来对应映射:

<强> XmlFile1.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ClassOne>
    <Field>Some Class 1 Field Value</Field>
</ClassOne>

<强> XmlFile2.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ClassTwo>
    <Field1>Some Class 2 Field Value</Field1>
    <Field2>100</Field2>
    <Field3>12.34</Field3>
</ClassTwo>

<强> XmlFile3.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ClassThree>
    <Field1>Some Class 3 Field Value</Field1>
    <Field2>Another Class 3 Field Value</Field2>
</ClassThree>

然后我创建了一个读取此文件的测试类,并将文件名和类名对应的类对象放入Map中,您可以选择将其用于任何您想要的内容,以及解组数据并打印出它读取的信息

public class TestClassMapper {
    public static void main(String[] args) {
        Map<String,Class> classMap = new HashMap<String, Class>();

        try {
            // reader for reading mapping file
            BufferedReader reader = new BufferedReader( new FileReader("mappings.txt"));

            String line;
            // read each line in the file
            while ( (line = reader.readLine()) != null ) {
                // split the data
                String[] parts = line.split(",");
                String fileName = parts[0]; // file name
                Class clazz = Class.forName(parts[1]); // class object
                classMap.put(fileName, clazz);  // you might want to do something with this later...

                // unmarshal the data
                JAXBContext context = JAXBContext.newInstance(clazz);
                Unmarshaller u = context.createUnmarshaller();
                // generic object
                Object o = u.unmarshal(new File(fileName));
                // print out the data received
                System.out.println(o.toString());
            }
        }
        catch (Exception ex) {

        }
    }
}

观察到以下输出:

ClassOne
    Field = Some Class 1 Field Value
ClassTwo
    Field1 = Some Class 2 Field Value
    Field2 = 100
    Field3 = 12.34
ClassThree
    Field1 = Some Class 3 Field Value
    Field2 = Another Class 3 Field Value
祝你好运,希望这会有所帮助。