2017-01-12 2 views
2

Ich versuche, ein XML-ile in einer Form zu erhalten:Java JAXB - get Ausgang bestimmte

<?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> 

Ich habe zwei Klassen: LineWrapper und Line:

LineWrapper:

@XmlRootElement(name = "image") 
public class LineWrapper { 

    private List<Line> lines; 

    @XmlElementWrapper(name = "lines") 
    @XmlElement(name = "line") 
    public List<Line> getLines() { 
     return lines; 
    } 
    (...) 
} 

Linie:

public class Line{ 
    (...) 
    @XmlAttribute(name = "id") 
    public String getId() { 
     return id.get(); 
    } 

    @XmlAttribute(name="x") 
    public List<Integer> getxList() { 
     return xList; 
    } 

    @XmlAttribute(name="y") 
    public List<Integer> getyList() { 
     return yList; 
    } 
} 

Was? Ich erhalte mit einer solchen Anmerkung ist:

<?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> 

Wie kann ich eine zusätzliche Markierung machen ‚Koordinaten‘ für ein Attribut Paar von x und y?

Antwort

0

erstellen Coordinates Klasse, um die x- und y-Werte halten:

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


} 

dann Ihre Line Klasse ändern, um eine Liste dieser als ein Element zu verwenden, sondern dass besitzen x und y-Werte:

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



} 

Natürlich müssen Sie richtige Coordinates Objekte erstellen (Setter-Methoden fehlen in diesem Beispiel), und füllen Sie Line Objekte mit ihnen.