Java/JAXB:根据属性将Xml解组为特定的子类

Fro*_*thy 29 java inheritance jaxb eclipselink

是否可以使用JAXB根据xml的属性将xml解组为特定的Java类?

<shapes>
  <shape type="square" points="4" square-specific-attribute="foo" />
  <shape type="triangle" points="3" triangle-specific-attribute="bar" />
</shapes>
Run Code Online (Sandbox Code Playgroud)

我想有一个包含三角形和正方形的Shape对象列表,每个对象都有自己的特定于形状的属性.IE:

abstract class Shape {
    int points;
    //...etc
}

class Square extends Shape {
    String square-specific-attribute;
    //...etc
}

class Triangle extends Shape {
    String triangle-specific-attribute;
    //...etc
}
Run Code Online (Sandbox Code Playgroud)

我目前只是将所有属性放在一个大的"Shape"类中,并且它不太理想.

如果形状被恰当地命名为xml元素,我可以让它工作,但不幸的是我无法控制我正在检索的xml.

谢谢!

bdo*_*han 18

JAXB是一个规范,具体实现将提供扩展点来做这样的事情.如果您使用的是EclipseLink JAXB(MOXy),则可以按如下方式修改Shape类:

import javax.xml.bind.annotation.XmlAttribute;
import org.eclipse.persistence.oxm.annotations.XmlCustomizer;

@XmlCustomizer(ShapeCustomizer.class)
public abstract class Shape {

    int points;

    @XmlAttribute
    public int getPoints() {
        return points;
    }

    public void setPoints(int points) {
        this.points = points;
    }

}
Run Code Online (Sandbox Code Playgroud)

然后使用MOXy @XMLCustomizer,您可以访问InheritancePolicy并将类指示符字段从"@xsi:type"更改为"type":

import org.eclipse.persistence.config.DescriptorCustomizer;
import org.eclipse.persistence.descriptors.ClassDescriptor;

public class ShapeCustomizer implements DescriptorCustomizer {

    @Override
    public void customize(ClassDescriptor descriptor) throws Exception {
        descriptor.getInheritancePolicy().setClassIndicatorFieldName("@type");
    }
}
Run Code Online (Sandbox Code Playgroud)

您将需要确保在模型类(Shape,Square等)中包含jaxb.properties文件,并使用以下条目指定EclipseLink MOXy JAXB实现:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Run Code Online (Sandbox Code Playgroud)

以下是其他模型类:

形状

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Shapes {

    private List<Shape> shape = new ArrayList<Shape>();;

    public List<Shape> getShape() {
        return shape;
    }

    public void setShape(List<Shape> shape) {
        this.shape = shape;
    }

}
Run Code Online (Sandbox Code Playgroud)

广场

import javax.xml.bind.annotation.XmlAttribute;

public class Square extends Shape {
    private String squareSpecificAttribute;

    @XmlAttribute(name="square-specific-attribute")
    public String getSquareSpecificAttribute() {
        return squareSpecificAttribute;
    }

    public void setSquareSpecificAttribute(String s) {
        this.squareSpecificAttribute = s;
    }

}
Run Code Online (Sandbox Code Playgroud)

三角形

import javax.xml.bind.annotation.XmlAttribute;

public class Triangle extends Shape {
    private String triangleSpecificAttribute;

    @XmlAttribute(name="triangle-specific-attribute")
    public String getTriangleSpecificAttribute() {
        return triangleSpecificAttribute;
    }

    public void setTriangleSpecificAttribute(String t) {
        this.triangleSpecificAttribute = t;
    }

}
Run Code Online (Sandbox Code Playgroud)

下面是一个演示程序,用于检查一切是否正常:

import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jaxbContext = JAXBContext.newInstance(Shapes.class, Triangle.class, Square.class);

        StringReader xml = new StringReader("<shapes><shape square-specific-attribute='square stuff' type='square'><points>4</points></shape><shape triangle-specific-attribute='triangle stuff' type='triangle'><points>3</points></shape></shapes>");
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        Shapes root = (Shapes) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助.

有关EclipseLink MOXy的更多信息,请参阅:

编辑

在EclipseLink 2.2中,我们使这更容易配置,请查看以下文章以获取更多信息:


小智 7

注释@XmlElements使您可以指定哪个标记与哪个子类相对应.

@XmlElements({
    @XmlElement(name="square", type=Square.class),
    @XmlElement(name="triangle", type=Triangle.class)
})
public List<Shape> getShape() {
    return shape;
}
Run Code Online (Sandbox Code Playgroud)

另请参阅@XmlElements的 javadoc