JAXB向Xml非根节点添加一个或多个属性

JAXB 向Xml非根节点添加一个或多个属性,直接上代码,关于JAXB的相关注解可查阅JAVA API。

原创文章,转载请注明出处。http://www.blogjava.net/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 阅读(10080) 评论(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  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2011年11月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

导航

统计

常用链接

留言簿

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜