xml与java双向映射

前端之家收集整理的这篇文章主要介绍了xml与java双向映射前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

java类:

@Entity
@Table(name = "sc_default_score_range")
public class DefaultscoreRange implements java.io.Serializable
{
private Integer id;
@Attribute
private double score;
@Attribute
private String scoreRange;

public DefaultscoreRange()
{
}

public DefaultscoreRange(double score,String scoreRange)
{
this.score = score;
this.scoreRange = scoreRange;
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id",unique = true,nullable = false)
public Integer getId()
{
return this.id;
}

public void setId(Integer id)
{
this.id = id;
}

@Column(name = "score",nullable = false,precision = 10)
public double getscore()
{
return this.score;
}

public void setscore(double score)
{
this.score = score;
}

@Column(name = "score_range",length = 200)
public String getscoreRange()
{
return this.scoreRange;
}

public void setscoreRange(String scoreRange)
{
this.scoreRange = scoreRange;
}

}

xml代码

<?xml version="1.0" encoding="utf-8"?>
<defaultscoreRangeList>

<defaultscoreRange score="150" scoreRange="35,54,72,90"/>
<defaultscoreRange score="100" scoreRange="24,36,48"/>
<defaultscoreRange score="90" scoreRange="22,32,43"/>
<defaultscoreRange score="60" scoreRange="15,22,29"/>

</defaultscoreRangeList>

xml与java映射类:

@Root
public class DefaultscoreRangeList
{
@ElementList(inline=true)
private List<DefaultscoreRange> defaultscoreRangeList = new ArrayList<DefaultscoreRange>();

public List<DefaultscoreRange> getDefaultscoreRangeList()
{
return defaultscoreRangeList;
}


public void setDefaultscoreRangeList(
List<DefaultscoreRange> defaultscoreRangeList)
{
this.defaultscoreRangeList = defaultscoreRangeList;
}
}

实现映射:

public static <ROOT> ROOT getXMLRoot(Class<ROOT> rootClass,String xmlclasspath){
Serializer serializer = new Persister();
ROOT root = null;
try {
root = serializer.read(rootClass,ResourceUtil.getResourceAsStream(xmlclasspath));
} catch (Exception e) {
throw new RuntimeException(e);
}
return root;
}

protected List<DefaultscoreRange> parse() { DefaultscoreRangeList rangeList = XMLUtil.getXMLRoot(DefaultscoreRangeList.class,"init/scDefaultscoreRange.xml"); return rangeList.getDefaultscoreRangeList(); }

猜你在找的XML相关文章