Java 扩展第三方现有JAXB类

Java 扩展第三方现有JAXB类,java,xml,jaxb,Java,Xml,Jaxb,我有一个第三方库,它提供了一组类。这些类包含XML结构的对象图,并使用JAXB进行XML(un)封送。假设我有汽车、车轴和轮胎的课程 汽车包含车轴列表,车轴包含轮胎列表 轮胎看起来像 public class Tire { private double width; private double radius; public double getWidth() { return width; } public double getRadiu

我有一个第三方库,它提供了一组类。这些类包含XML结构的对象图,并使用JAXB进行XML(un)封送。假设我有汽车、车轴和轮胎的课程

汽车包含车轴列表,车轴包含轮胎列表

轮胎看起来像

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

    public double getWidth() {
       return width;
    }
    public double getRadius() {
       return radius;
    }
}
现在我想给轮胎添加一个叫做压力的属性

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来完成这项工作?

是的,您可以这样做,而且

下面的例子取自上面的链接:(你所需要做的就是@Override对象创建也。如果你错过了它,它将不起作用)

package org.acme.foo.impl;
类PersonEx扩展人{
@凌驾
公共void集合名(字符串名){

如果(name.length()我想这应该行得通

我刚刚尝试了以下课程:

@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;
    }
}
@xmlacessortype(xmlacesstype.FIELD)
公营轮胎
{
双倍宽度;
双半径;
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
公营车轴
{
@xmlement(name=“tire”)
列表轮胎=新的ArrayList();
}
@XmlAccessorType(XmlAccessType.FIELD)
公共级增压轮胎
{
双重压力;
公共增压轮胎(t型轮胎,双压)
{
此宽度=t宽度;
这个半径=t半径;
这个。压力=压力;
}
}
和下面的代码

    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 );
JAXBContext context=JAXBContext.newInstance(axex.class,pressuredTire.class);
Unmarshaller Unmarshaller=context.createUnmarshaller();
字符串xml=“2.01.52.51.5”;
车轴=(车轴)解组器.解组器(新StringReader(xml));
列出加压电缆=新的ArrayList();
用于(轮胎:车轴.轮胎)
{
加压轮胎。添加(新加压轮胎(轮胎,1.0));
}
车轴。轮胎=加压轮胎;
Marshaller=context.createMarshaller();
setProperty(marshaller.JAXB_格式化的_输出,true);
编组员.编组员(车轴,系统输出);
得到了这个输出:

<?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>

2
1.5
1
2.5
1.5
1
<?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>