栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java / JAXB:基于属性将XML解组到特定子类

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java / JAXB:基于属性将XML解组到特定子类

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;    }}

然后使用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");    }}

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

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

下面是其余的模型类:

形状

import java.util.ArrayList;import java.util.List;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElementpublic class Shapes {    private List<Shape> shape = new ArrayList<Shape>();;    public List<Shape> getShape() {        return shape;    }    public void setShape(List<Shape> shape) {        this.shape = shape;    }}

广场

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;    }}

三角形

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;    }}

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

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);    }}

我希望这有帮助。

有关Eclipselink MOXy的更多信息,请参见:

  • http://www.eclipse.org/eclipselink/moxy.php

编辑

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

  • http://bdoughan.blogspot.com/2010/11/jaxb-and-inheritance-moxy-extension.html


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/449609.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号