<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    JAXB向Xml非根節點添加一個或多個屬性

    JAXB 向Xml非根節點添加一個或多個屬性,直接上代碼,關于JAXB的相關注解可查閱JAVA API。

    原創文章,轉載請注明出處。http://www.tkk7.com/kangdy/archive/2011/11/23/364635.html

    code1: colors類  根節點
    code1
    package com.kangdy.test;

    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;

    @XmlRootElement(name = "Colors")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Colors {
        
        @XmlElement(name = "red",nillable=true)
        private Red red;
        
        @XmlElement(name = "blue",nillable=true)
        private Blue blue;

        public Red getRed() {
            return red;
        }

        public Blue getBlue() {
            return blue;
        }

        public void setRed(Red red) {
            this.red = red;
        }

        public void setBlue(Blue blue) {
            this.blue = blue;
        }
    }

    code2:  Red類  子節點
    code2package com.kangdy.test;

    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlRootElement;

    @XmlRootElement(name = "red")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Red {
        
        private String value;
        
        @XmlAttribute(name = "att1")
        private String att;
        
        public String getValue() {
            return value;
        }
        
        public void setValue(String value) {
            this.value = value;
        }

        public String getAtt() {
            return att;
        }

        public void setAtt(String att) {
            this.att = att;
        }
        
    }


    code3:  類 Blue 子節點
    code3
    package com.kangdy.test;

    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlRootElement;

    @XmlRootElement(name = "blue")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Blue {
        private String value;
        
        @XmlAttribute(name = "att2")
        private String att2;
        
        @XmlAttribute(name = "att1")
        private String att;
        
        public String getAtt() {
            return att;
        }

        public void setAtt(String att) {
            this.att = att;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public String getAtt2() {
            return att2;
        }

        public void setAtt2(String att2) {
            this.att2 = att2;
        }
    }

    code4: main類
    code4
    package com.kangdy.test;

    import java.io.StringWriter;

    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Marshaller;

    public class Jaxbtest {
        public static void main(String[] args) throws Exception {

            StringWriter writer = new StringWriter();
            JAXBContext jc = JAXBContext.newInstance(Colors.class);
            Marshaller ma = jc.createMarshaller();
            ma.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            
            Colors colors = new Colors();
            Red red = new Red();
            red.setAtt("att-red");
            red.setValue("red");
            Blue blue = new Blue();
            blue.setValue("blue");
            blue.setAtt("att-blue");
            blue.setAtt2("blue-att2");
            colors.setRed(red);
            colors.setBlue(blue);
            
            ma.marshal(colors, writer);
            System.out.println(writer.toString());

        }
    }

    運行結果:
    結果
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <Colors>
        <red att1="att-red">
            <value>red</value>
        </red>
        <blue att1="att-blue" att2="blue-att2">
            <value>blue</value>
        </blue>
    </Colors>

    posted on 2011-11-23 14:33 AK47 閱讀(10126) 評論(4)  編輯  收藏 所屬分類: java相關

    評論

    # re: JAXB向Xml非根節點添加一個或多個屬性[未登錄] 2014-01-14 10:08 lili

    1234  回復  更多評論   

    # re: JAXB向Xml非根節點添加一個或多個屬性 2014-08-06 11:22 TTBear

    請問一下想<error code="01">dsds</error>這種格式的怎么取出屬性code  回復  更多評論   

    # re: JAXB向Xml非根節點添加一個或多個屬性 2016-05-20 15:40 @jason

    那怎么給根節點增加屬性呢?  回復  更多評論   

    # re: JAXB向Xml非根節點添加一個或多個屬性 2016-08-09 15:00 12

    12  回復  更多評論   

    <2016年8月>
    31123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910

    導航

    統計

    常用鏈接

    留言簿

    隨筆分類

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲国产成人综合精品| 最新亚洲精品国偷自产在线| 免费很黄无遮挡的视频毛片| 最新免费jlzzjlzz在线播放| 亚洲国产av美女网站| 91香蕉国产线观看免费全集| 婷婷亚洲久悠悠色悠在线播放 | 久久久久久久亚洲精品| 精品女同一区二区三区免费播放| 日本高清色本免费现在观看| 亚洲人成色在线观看| 在线观看免费a∨网站| 亚洲GV天堂无码男同在线观看| 免费看的黄色大片| 四虎一区二区成人免费影院网址 | 国产在线精品观看免费观看| 国产亚洲精品AA片在线观看不加载| 麻豆一区二区三区蜜桃免费| 亚洲一区二区三区在线播放 | 成人福利免费视频| 亚洲乱码一区二区三区国产精品| 西西大胆无码视频免费| 久久精品国产亚洲AV天海翼 | 97公开免费视频| 亚洲国产成人精品无码区在线秒播 | 亚洲成人福利在线观看| 91嫩草国产在线观看免费| 亚洲色欲色欱wwW在线| 免费一级毛片在线播放不收费| h视频免费高清在线观看| 久久久久久亚洲精品中文字幕| 1a级毛片免费观看| 亚洲AV成人片无码网站| 国产亚洲精品岁国产微拍精品| 久久久久高潮毛片免费全部播放 | 久久久久久久久久免免费精品| 亚洲中文字幕精品久久| 免费中文字幕不卡视频| 免费观看在线禁片| 亚洲欧美日韩综合久久久| 亚洲欧洲日产国码无码网站 |