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

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

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

    JAXB向Xml非根節(jié)點(diǎn)添加一個(gè)或多個(gè)屬性

    JAXB 向Xml非根節(jié)點(diǎn)添加一個(gè)或多個(gè)屬性,直接上代碼,關(guān)于JAXB的相關(guān)注解可查閱JAVA API。

    原創(chuàng)文章,轉(zhuǎn)載請(qǐng)注明出處。http://www.tkk7.com/kangdy/archive/2011/11/23/364635.html

    code1: colors類  根節(jié)點(diǎn)
    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類  子節(jié)點(diǎn)
    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 子節(jié)點(diǎn)
    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());

        }
    }

    運(yùn)行結(jié)果:
    結(jié)果
    <?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) 評(píng)論(4)  編輯  收藏 所屬分類: java相關(guān)

    評(píng)論

    # re: JAXB向Xml非根節(jié)點(diǎn)添加一個(gè)或多個(gè)屬性[未登錄] 2014-01-14 10:08 lili

    1234  回復(fù)  更多評(píng)論   

    # re: JAXB向Xml非根節(jié)點(diǎn)添加一個(gè)或多個(gè)屬性 2014-08-06 11:22 TTBear

    請(qǐng)問一下想<error code="01">dsds</error>這種格式的怎么取出屬性code  回復(fù)  更多評(píng)論   

    # re: JAXB向Xml非根節(jié)點(diǎn)添加一個(gè)或多個(gè)屬性 2016-05-20 15:40 @jason

    那怎么給根節(jié)點(diǎn)增加屬性呢?  回復(fù)  更多評(píng)論   

    # re: JAXB向Xml非根節(jié)點(diǎn)添加一個(gè)或多個(gè)屬性 2016-08-09 15:00 12

    12  回復(fù)  更多評(píng)論   

    <2014年8月>
    272829303112
    3456789
    10111213141516
    17181920212223
    24252627282930
    31123456

    導(dǎo)航

    統(tǒng)計(jì)

    常用鏈接

    留言簿

    隨筆分類

    隨筆檔案

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 日本视频一区在线观看免费| 亚洲黄色片免费看| 亚洲VA成无码人在线观看天堂| 99热这里有免费国产精品| 亚洲另类自拍丝袜第1页| 亚洲av高清在线观看一区二区| 国产在线国偷精品免费看| 亚洲欧洲久久精品| 亚洲av午夜精品一区二区三区| 男人的天堂网免费网站| 亚洲国产精品无码第一区二区三区 | 亚洲综合无码无在线观看| 成年在线网站免费观看无广告| 女bbbbxxxx另类亚洲| 亚洲精品无码久久久久去q| 国产黄色免费网站| 黄色片网站在线免费观看| 亚洲精品免费在线观看| 免费中文字幕在线观看| 18女人腿打开无遮掩免费| 日本一区二区三区免费高清在线| 亚洲午夜在线电影| 亚洲一区二区高清| 国内免费高清在线观看| 九九精品成人免费国产片| 亚洲1区2区3区精华液| 久久久久亚洲精品无码蜜桃| 亚洲性久久久影院| 免费无码看av的网站| 日本免费人成视频在线观看| 曰韩无码AV片免费播放不卡| 亚洲AV成人噜噜无码网站| 亚洲乱码国产乱码精品精| 国产大片51精品免费观看| 4虎永免费最新永久免费地址| 亚洲国产免费综合| 国产亚洲精品美女久久久久久下载| 精品亚洲aⅴ在线观看| 亚洲中文字幕无码日韩| 久久亚洲精品高潮综合色a片| 久久精品国产亚洲AV|