简体   繁体   English

Jaxb Xml到java.awt

[英]Jaxb Xml to java.awt

I'm using JAXB for XML biding in my Project, I'm still a beginner. 我在Project中使用JAXB进行XML出价,但我仍然是一个初学者。 Now i try to bind this XML File to the Class FontStyle 现在,我尝试将此XML文件绑定到Class FontStyle

The Xml File looks like this Xml文件如下所示

example.xml example.xml

<fontStyle>
  <font>
    <family>Arial</family>
    <style>0</style>
    <size>12</size>
  </font>
  <fontColor>
    <red>0</red>
    <green>0</green>
    <blue>0</blue>
  </fontColor>
  <backgroundColor>
    <red>255</red>
    <green>255</green>
    <blue>255</blue>
  </backgroundColor>
</fontStyle>

This is my FontStyle Class: 这是我的FontStyle类别:

FontStyle.java FontStyle.java

import java.awt.Color;
import java.awt.Font;


public class FontStyle {

    private Font font;

    private Color fontColor = Color.BLACK;

    private Color backgroundColor = Color.WHITE;

    public FontStyle() {

    }

    public FontStyle(Font font, Color fontColor, Color backgroundColor) {
        this.font = font;
        this.fontColor = fontColor;
        this.backgroundColor = backgroundColor;
    }

    public Font getFont() {
        return font;
    }


    public void setFont(Font font) {
        this.font = font;
    }

    public Color getFontColor() {
        return fontColor;
    }


    public void setFontColor(Color fontColor) {
        this.fontColor = fontColor;
    }


    public Color getBackgroundColor() {
        return backgroundColor;
    }


    public void setBackgroundColor(Color backgroundColor) {
        this.backgroundColor = backgroundColor;
    }


}

I hope anyone can give me an advise how to handle this. 我希望任何人都可以给我建议如何处理。

cheers 干杯

Types that do not map naturally to a XML representation require writing an XmlAdapter implementation, Font and Color are such types. 无法自然映射到XML表示形式的XmlAdapter需要编写XmlAdapter实现,而FontColor就是这种类型。 The code below presents an example of how you can write adapters in your case. 下面的代码提供了一个如何在您的情况下编写适配器的示例。

I placed the adapter classes as nested classes in FontStyle class but you can create them as external classes if you wish. 我将适配器类作为嵌套类放置在FontStyle类中,但是您可以根据需要将它们创建为外部类。

import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.awt.*;

@XmlRootElement
@XmlType(propOrder = {"font", "fontColor", "backgroundColor"}) // to keep ordering consistent with "example.xml"
public class FontStyle {

    private Font font;

    private Color fontColor = Color.BLACK;

    private Color backgroundColor = Color.WHITE;

    public FontStyle() {
    }

    public FontStyle(Font font, Color fontColor, Color backgroundColor) {
        this.font = font;
        this.fontColor = fontColor;
        this.backgroundColor = backgroundColor;
    }

    @XmlJavaTypeAdapter(FontAdapter.class)
    public Font getFont() {
        return font;
    }

    @XmlJavaTypeAdapter(ColorAdapter.class)
    public Color getFontColor() {
        return fontColor;
    }

    @XmlJavaTypeAdapter(ColorAdapter.class)
    public Color getBackgroundColor() {
        return backgroundColor;
    }

    public void setFont(Font font) {
        this.font = font;
    }

    public void setFontColor(Color fontColor) {
        this.fontColor = fontColor;
    }

    public void setBackgroundColor(Color backgroundColor) {
        this.backgroundColor = backgroundColor;
    }

    private static class ColorAdapter extends XmlAdapter<ColorAdapter.ColorValueType, Color> {

        @Override
        public Color unmarshal(ColorValueType v) throws Exception {
            return new Color(v.red, v.green, v.blue);
        }

        @Override
        public ColorValueType marshal(Color v) throws Exception {
            return new ColorValueType(v.getRed(), v.getRed(), v.getBlue());
        }

        @XmlAccessorType(XmlAccessType.FIELD)
        public static class ColorValueType {
            private int red;
            private int green;
            private int blue;

            public ColorValueType() {
            }

            public ColorValueType(int red, int green, int blue) {
                this.red = red;
                this.green = green;
                this.blue = blue;
            }
        }
    }

    private static class FontAdapter extends XmlAdapter<FontAdapter.FontValueType, Font> {

        @Override
        public Font unmarshal(FontValueType v) throws Exception {
            return new Font(v.family, v.style, v.size);
        }

        @Override
        public FontValueType marshal(Font v) throws Exception {
            return new FontValueType(v.getFamily(), v.getStyle(), v.getSize());
        }

        @XmlAccessorType(XmlAccessType.FIELD)
        public static class FontValueType {
            private String family;
            private int style;
            private int size;

            public FontValueType() {
            }

            public FontValueType(String family, int style, int size) {
                this.family = family;
                this.style = style;
                this.size = size;
            }
        }
    }
}

The code to unmarshall example.xml and test the result can look like this: 解编example.xml并测试结果的代码如下所示:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.File;

public class App {
    public static void main(String[] args) throws JAXBException {
        JAXBContext jaxbContext = JAXBContext.newInstance(FontStyle.class);

        // unmarshall "example.xml"
        File exampleFile = new File("example.xml");
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        FontStyle fontStyle = (FontStyle) jaxbUnmarshaller.unmarshal(exampleFile);

        // marshall back to XML and print the result
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); // removes xml declaration line for consistency with "example.xml" file
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.marshal(fontStyle, System.out);
    }
}

Marcin's answer works perfectly but please be aware of the following line of code: Marcin的答案非常有效,但请注意以下代码行:

@Override
public ColorValueType marshal(Color v) throws Exception {
    return new ColorValueType(v.getRed(), v.getRed(), v.getBlue());
}

If you try to edit the green in the XML it will cause it to be overwritten with the red value since it's calling the v.getRed() twice. 如果您尝试在XML中编辑绿色,则会导致绿色被红色值覆盖,因为它两次调用了v.getRed()

This would be the correct code. 这将是正确的代码。

@Override
public ColorValueType marshal(Color v) throws Exception {
    return new ColorValueType(v.getRed(), v.getGreen(), v.getBlue()); // Changed to getGreen().
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM