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