未调用JAXB XmlJavaTypeAdapter unmarshal方法

时间:2013-05-14 15:24:03

标签: xml-parsing jaxb

我正在尝试编组/解组Color to XML。 JAXB项目有一个示例代码,用于通过XmlJavaTypeAdapter https://jaxb.java.net/guide/XML_layout_and_in_memory_data_layout.html执行此操作。

编组工作正常,输出符合我的预期:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<beanWithColor>
    <foreground>#ff0000ff</foreground>
    <name>CleverName</name>
</beanWithColor>

但是,当尝试从XML转到对象时,永远不会调用unmarshal方法。谁能提供有关原因的见解?在解组后,前景色为空,我已经通过调试器确认从未调用过unmarshal:

BeanWithColor{foreground=null, name='CleverName'}

SCCE:

@XmlRootElement
public class BeanWithColor {
private String name;
private Color foreground;

public BeanWithColor() {

}

public BeanWithColor(Color foreground, String name) {
    this.foreground = foreground;
    this.name = name;
}

@XmlJavaTypeAdapter(ColorAdapter.class)
public Color getForeground() {
    return this.foreground;
}

public void setForeground(Color foreground) {
    this.foreground = foreground;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Override
public String toString() {
    final StringBuilder sb = new StringBuilder("BeanWithColor{");
    sb.append("foreground=").append(foreground);
    sb.append(", name='").append(name).append('\'');
    sb.append('}');
    return sb.toString();
}

public static void main(String[] args) {
    BeanWithColor bean = new BeanWithColor(Color.blue, "CleverName");

    try {
        StringWriter writer = new StringWriter(1000);
        JAXBContext context = JAXBContext.newInstance(BeanWithColor.class);
        final Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(bean, writer);
        System.out.println("Marshaled XML: " + writer);

        final Unmarshaller unmarshaller = context.createUnmarshaller();
        BeanWithColor beanWithColor = (BeanWithColor) unmarshaller.unmarshal(new StringReader(writer.toString()));
        System.out.println("beanWithColor = " + beanWithColor);
    } catch (JAXBException e) {
        e.printStackTrace();
    }

}

static class ColorAdapter extends XmlAdapter<String, Color> {
    public Color unmarshal(String s) {
        return Color.decode(s);
    }

    public String marshal(Color color) {
        return '#' + Integer.toHexString(color.getRGB());
    }
}
}

1 个答案:

答案 0 :(得分:2)

我怀疑XmlAdapter正在调用unmarshal,但Color.decode方法失败了(这是调试代码时发生的情况)。

Color.decode("#ff0000ff");

导致以下结果:

Exception in thread "main" java.lang.NumberFormatException: For input string: "ff0000ff"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:461)
    at java.lang.Integer.valueOf(Integer.java:528)
    at java.lang.Integer.decode(Integer.java:958)
    at java.awt.Color.decode(Color.java:707)

您可以在ValidationEventHandler上设置Unmarshaller以获取所有失败的挂钩。默认情况下,JAXB impl不会报告该问题。

修复

需要修复ColorAdapter.marshal方法以返回正确的值。

String rgb = Integer.toHexString(color.getRGB());
return "#" + rgb.substring(2, rgb.length());