当类为XmlAccessType.NONE时,如何JAXB-marshall JavaFX属性类? [英] How to JAXB-marshall JavaFX Property classes when class is XmlAccessType.NONE?

查看:100
本文介绍了当类为XmlAccessType.NONE时,如何JAXB-marshall JavaFX属性类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用JAXB的marshall()方法调用将包含JavaFX属性对象(DoubleProperty,StringProperty,IntegerProperty)的Java类写入XML文件.但是,此类包含许多我不想写入XML的数据.预期此类通常会被开发人员修改,因此我更喜欢将类标记为"@XmlAccessorType(XmlAccessType.NONE)",然后将@XmlElement标记添加到我要写入XML的任何内容中(因此开发人员不会添加一些内容)新的成员变量添加到此类中,然后不小心更改了XML文件的格式.

I would like a Java class containing JavaFX Property objects (DoubleProperty, StringProperty, IntegerProperty) to be written into an XML file using JAXB's marshall() method call. However, this class contains lots of data that I do not want written into the XML. This class is expected to be modified by developers often, so I prefer to mark the class "@XmlAccessorType(XmlAccessType.NONE)" and then add @XmlElement tags to anything I want written into the XML (so a developer doesn't add some new member variables into this class and then accidentally alter the XML file's format).

我看到了诸如 http://blog的示例.bdoughan.com/2010/11/jaxb-and-inheritance-using-xsitype.html ,但是它们的主类都没有"@XmlAccessorType(XmlAccessType.NONE)".当我将此标签添加到类中时,会得到运行时异常(因为JAXB无法创建新对象)或输出中的空XML标签(因为JAXB创建了某种类型的默认对象,但未将所需的值放入它).我已经尝试了数十种@ Xml *标签组合,但找不到有效的组合.

I see examples such as http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-xsitype.html, but none of them have "@XmlAccessorType(XmlAccessType.NONE) " for their main class. When I add this tag to my class, I get either runtime Exceptions (because JAXB cannot create a new object) or an empty XML tag in the output (because JAXB created a default object of ome kind but didn't put the desired value into it). I have experimentes with dozens of @Xml* tag combinations but I cannot find one that works.

请注意,我无法注释任何DoubleProperty/SimpleDoubleProperty类,因为它们是标准Java API的一部分.

Note that I cannot annotate any of the DoubleProperty/SimpleDoubleProperty classes because they are part of the standard Java API.

这是一个代码示例,演示了将bankAccountBalance属性放入XML文件的麻烦. (您可以忽略其他数据-我以Blaise Doughan的代码作为该示例的起点).

Here is a code example, demonstrating the troubles with getting the bankAccountBalance property into the XML file. (you can ignore the other data - I started with Blaise Doughan's code as a starting-point for this example).

package Demo2;

import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;

public class Demo2 {

    public static void main(String[] args) throws Exception {
        Customer customer = new Customer();
        Address address = new Address();
        address.setStreet("1 A Street");
        customer.setContactInfo(address);
        customer.setBankAccountBalance(123.45);
        JAXBContext jc = JAXBContext.newInstance(Customer.class, Address.class, PhoneNumber.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(customer, System.out);
    }
}

abstract class ContactInfo {
}

class Address extends ContactInfo {
    private String street;

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }
}

class PhoneNumber extends ContactInfo {
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE) 
class Customer {
    @XmlElement
    private ContactInfo contactInfo;
    // This tag runs OK but always outputs an empty XML tag ("<bankAccountBalance/>") regardless of the real value.
//    @XmlElement
    // This tag causes the following error:
    //     Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
    //     Invalid @XmlElementRef : Type "class javafx.beans.property.DoubleProperty" or any of its subclasses are not known to this context.
//    @XmlElementRef
    // This tag causes the following error:
    //     Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
    //     Invalid @XmlElementRef : Type "class javafx.beans.property.SimpleDoubleProperty" or any of its subclasses are not known to this context.
//    @XmlElementRef(type=SimpleDoubleProperty.class)
    private DoubleProperty bankAccountBalance;

    public Customer() {
        bankAccountBalance = new SimpleDoubleProperty(0);
    }

    public ContactInfo getContactInfo() {
        return contactInfo;
    }

    public void setContactInfo(ContactInfo contactInfo) {
        this.contactInfo = contactInfo;
    }

    public double getBankAccountBalance() {
        return bankAccountBalance.get();
    }

    public void setBankAccountBalance(double bankAccountBalance) {
        this.bankAccountBalance.set(bankAccountBalance);
    }
}

推荐答案

仅注释getter而不是DoubleProperty字段,该字段很好地绕过了Javafx类.不要忘记设置setValue,这样您就可以看到结果.

Simply annotate the getter and not the DoubleProperty field, which nicely bypasses the javafx class. Don't forget to setValue so you see the result.


    @XmlElement
    public double getBankAccountBalance() {
        return bankAccountBalance.get();
    }

这篇关于当类为XmlAccessType.NONE时,如何JAXB-marshall JavaFX属性类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆