javajaxb-获取预期输出
我正在尝试以以下形式获取XML文件:javajaxb-获取预期输出,java,xml,jaxb,Java,Xml,Jaxb,我正在尝试以以下形式获取XML文件: <?xml version="1.0" encoding="UTF-8"?> <image> <lines> <line id="a"> <coordinates x="297" y="44"/> <coordinates x="302" y="117"/> </line &
<?xml version="1.0" encoding="UTF-8"?>
<image>
<lines>
<line id="a">
<coordinates x="297" y="44"/>
<coordinates x="302" y="117"/>
</line >
<line id="b">
<coordinates x="42" y="111"/>
<coordinates x="48" y="131"/>
<coordinates x="39" y="142"/>
</line >
</lines>
</image>
我有两个类:LineWrapper和Line:
换行器:
@XmlRootElement(name = "image")
public class LineWrapper {
private List<Line> lines;
@XmlElementWrapper(name = "lines")
@XmlElement(name = "line")
public List<Line> getLines() {
return lines;
}
(...)
}
@XmlRootElement(name=“image”)
公共类换行器{
私有列表行;
@XmlElementWrapper(name=“lines”)
@xmlement(name=“line”)
公共列表getLines(){
回流线;
}
(...)
}
行:
公共类行{
(...)
@xmltattribute(name=“id”)
公共字符串getId(){
返回id.get();
}
@xmltattribute(name=“x”)
公共列表getxList(){
返回xList;
}
@xmltattribute(name=“y”)
公共列表getyList(){
返回yList;
}
}
我从这种注释中得到的是:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<image>
<lines>
<line id="a" x="297 302" y="44 117"/>
<line id="b" x="44 48 39" y="111 131 142"/>
</lines>
</image>
如何为x和y属性对创建额外的标记“坐标”?创建一个包含x和y值的
坐标类:
public class Coordinates{
(...)
private int x;
private int y;
@XmlAttribute(name="x")
public int getX() {
return x;
}
@XmlAttribute(name="y")
public int getY() {
return y;
}
}
public class Line{
(...)
private List<Coordinates> coordinatesList = new ArrayList<Coordinates>;
@XmlAttribute(name = "id")
public String getId() {
return id.get();
}
@XmlElement(name = "coordinates")
public List<Coordinates> getCoordinatesList() {
return coordinatesList;
}
}
然后修改行
类,将其列表用作元素,而不是拥有x和y值的元素:
public class Coordinates{
(...)
private int x;
private int y;
@XmlAttribute(name="x")
public int getX() {
return x;
}
@XmlAttribute(name="y")
public int getY() {
return y;
}
}
public class Line{
(...)
private List<Coordinates> coordinatesList = new ArrayList<Coordinates>;
@XmlAttribute(name = "id")
public String getId() {
return id.get();
}
@XmlElement(name = "coordinates")
public List<Coordinates> getCoordinatesList() {
return coordinatesList;
}
}
公共类行{
(...)
私有列表协调列表=新建ArrayList;
@xmltattribute(name=“id”)
公共字符串getId(){
返回id.get();
}
@xmlement(name=“坐标”)
公共列表GetCoordinates列表(){
返回协调列表;
}
}
当然,您必须创建适当的坐标
对象(本例中缺少setter方法),并用它们填充Line
对象