簡體   English   中英

Jaxb Xml到java.awt

[英]Jaxb Xml to java.awt

我在Project中使用JAXB進行XML出價,但我仍然是一個初學者。 現在,我嘗試將此XML文件綁定到Class FontStyle

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>

這是我的FontStyle類別:

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;
    }


}

我希望任何人都可以給我建議如何處理。

干杯

無法自然映射到XML表示形式的XmlAdapter需要編寫XmlAdapter實現,而FontColor就是這種類型。 下面的代碼提供了一個如何在您的情況下編寫適配器的示例。

我將適配器類作為嵌套類放置在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;
            }
        }
    }
}

解編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的答案非常有效,但請注意以下代碼行:

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

如果您嘗試在XML中編輯綠色,則會導致綠色被紅色值覆蓋,因為它兩次調用了v.getRed()

這將是正確的代碼。

@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