Java unmarshaller 系列2

Bean有注解

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

@XmlRootElement(name = "config")
@XmlAccessorType (XmlAccessType.FIELD)

public class Config {
    private long dashboardTvId ; 
    private int freshTime ;
    private long monitorGroupId ;
    private int timeOfPareto ;
    private int numOfPointer ;
    private String name ; 
    private String title ; 
    private String theme ;
    public long getDashboardTvId() {
        return dashboardTvId;
    }
    public void setDashboardTvId(long dashboardTvId) {
        this.dashboardTvId = dashboardTvId;
    }
    public int getFreshTime() {
        return freshTime;
    }
    public void setFreshTime(int freshTime) {
        this.freshTime = freshTime;
    }
    public long getMonitorGroupId() {
        return monitorGroupId;
    }
    public void setMonitorGroupId(long l) {
        this.monitorGroupId = l;
    }
    public int getTimeOfPareto() {
        return timeOfPareto;
    }
    public void setTimeOfPareto(int timeOfPareto) {
        this.timeOfPareto = timeOfPareto;
    }
    public int getNumOfPointer() {
        return numOfPointer;
    }
    public void setNumOfPointer(int numOfPointer) {
        this.numOfPointer = numOfPointer;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getTheme() {
        return theme;
    }
    public void setTheme(String theme) {
        this.theme = theme;
    }
    @Override
    public String toString() {
        return "Config [dashboardTvId=" + dashboardTvId + ", freshTime=" + freshTime + ", monitorGroupId="
                + monitorGroupId + ", timeOfPareto=" + timeOfPareto + ", numOfPointer=" + numOfPointer + ", name="
                + name + ", title=" + title + ", theme=" + theme + "]";
    }
}

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

public class OneObject {

    public static void main(String[] args) {

        StringReader strdata = new StringReader("  <config>\n"
                + "     <dashboardTvId>1</dashboardTvId><freshTime>10</freshTime><monitorGroupId>1</monitorGroupId><timeOfPareto>1</timeOfPareto><numOfPointer>1</numOfPointer><name>name</name><title>title</title><theme>black</theme>\n"
                + "  </config>");
        try {
            StreamSource streamSource = new StreamSource(strdata);
            JAXBContext jc = JAXBContext.newInstance(Config.class);
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Config conf2 = (Config) unmarshaller.unmarshal(streamSource);
            System.out.println(conf2.toString());
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值