扩展第三方现有的JAXB类

时间:2015-05-08 15:16:44

标签: java xml jaxb

我有一个提供一组课程的第三方库。这些类包含XML结构的对象图,并使用JAXB进行XML(un)编组。让我们说我有班级汽车,车轴和轮胎。

Car包含一个Axle和Axle列表,其中包含一个Tire列表。

轮胎看起来像​​

public class Tire {
    private double width;
    private double radius;

    public double getWidth() {
       return width;
    }
    public double getRadius() {
       return radius;
    }
}

现在我想为Tire添加一个名为pressure的属性

public class PressurizedTire extends Tire {
    private double pressure;

    public PressurizedTire(Tire t, double pressure) {
      this.width = t.getWidth();
      this.radius = t.getRadius();
      this.pressure = pressure;
    }
 }

我想使用JAXB对包含汽车的xml文档进行反序列化,找到所有轮胎并将压力数据添加到每个轮胎中。然后,我想将消息重新序列化为XML。目前,在编组时,额外的财产压力会下降,并且物体结构会恢复原状。

完成向现有JAXB模型类添加属性的最佳方法是什么?我不想扩展每个课程,我不确定重建和修改.xsd来实现这个目标吗?

2 个答案:

答案 0 :(得分:1)

是的,你可以这样做,it is called Adding behavior to existing JAXB classes

下面的例子来自上面的链接:(所有你需要做的就是@Override创建对象。如果你错过它,它将无法工作)。

package org.acme.foo.impl;

class PersonEx extends Person {
  @Override
  public void setName(String name) {
    if(name.length()<3) throw new IllegalArgumentException();
    super.setName(name);
  }
}

@XmlRegistry
class ObjectFactoryEx extends ObjectFactory {
  @Override
  Person createPerson() {
    return new PersonEx();
  }
}

答案 1 :(得分:1)

我认为这应该有用

我刚试过以下课程:

@XmlAccessorType( XmlAccessType.FIELD )
public class Tire
{
    double width;
    double radius;
}

@XmlRootElement
@XmlAccessorType( XmlAccessType.FIELD )
public class Axle
{
    @XmlElement( name = "tire" )
    List<Tire> tires = new ArrayList<>();
}

@XmlAccessorType( XmlAccessType.FIELD )
public class PressurizedTire extends Tire
{
    double pressure;

    public PressurizedTire( Tire t, double pressure )
    {
        this.width = t.width;
        this.radius = t.radius;
        this.pressure = pressure;
    }
}

和以下代码

    JAXBContext context = JAXBContext.newInstance( Axle.class, PressurizedTire.class );
    Unmarshaller unmarshaller = context.createUnmarshaller();

    String xml = "<axle><tire><width>2.0</width><radius>1.5</radius></tire><tire><width>2.5</width><radius>1.5</radius></tire></axle>";
    Axle axle = (Axle) unmarshaller.unmarshal( new StringReader( xml ) );

    List<Tire> pressurizedTires = new ArrayList<>();
    for ( Tire tire : axle.tires )
    {
        pressurizedTires.add( new PressurizedTire( tire, 1.0 ) );
    }
    axle.tires = pressurizedTires;

    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );
    marshaller.marshal( axle, System.out );

得到了这个输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<axle>
    <tire xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="pressurizedTire">
        <width>2.0</width>
        <radius>1.5</radius>
        <pressure>1.0</pressure>
    </tire>
    <tire xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="pressurizedTire">
        <width>2.5</width>
        <radius>1.5</radius>
        <pressure>1.0</pressure>
    </tire>
</axle>