0

I should read every value of Turn element in the input XML:

<Section type="report" startTime="0" endTime="182.952">
        <Turn speaker="spk1" startTime="7.186" endTime="8.114">
            <Sync time="7.186"/>un souci avec une inscription
        </Turn>
        <Turn speaker="spk2" startTime="8.114" endTime="8.533">
            <Sync time="8.114"/>ouais
        </Turn>
        <Turn speaker="spk1 spk2" startTime="8.533" endTime="9.731">
            <Sync time="8.533"/>
            <Who nb="1"/>first value!
            <Who nb="2"/>second value!
        </Turn>
</Section> 

So I used JAXB and made the following classes:

Section:

@XmlRootElement(name="Section")
public class Section {

private List<Turn> turn;

@XmlElement(name="Turn")
public List<Turn> getTurn() {
    if(turn == null){
        turn = new ArrayList<Turn>();
    }
    return turn;
}

public void setTurn(List<Turn> turn) {
    this.turn = turn;
}
}

Turn:

@XmlRootElement(name="Turn")
public class Turn {

private String speaker;
private float startTime;
private float endTime;
private Sync sync;
private String content;
private List<Who> whoList;

@XmlAttribute
public String getSpeaker() {
    return speaker;
}
public void setSpeaker(String speaker) {
    this.speaker = speaker;
}
public float getStartTime() {
    return startTime;
}

@XmlAttribute
public void setStartTime(float startTime) {
    this.startTime = startTime;
}

@XmlAttribute
public float getEndTime() {
    return endTime;
}
public void setEndTime(float endTime) {
    this.endTime = endTime;
}

@XmlValue
public String getContent() {
    return content;
}
public void setContent(String content) {
    this.content = content;
}
}

But when I want to read for example the value of Turn where speaker equals "spk1 spk2", the method getContent of Turn return only "second value!".. How i can get all content with "first value!" ? I know is not allowed to set XmlElement with XmlValue for one Element, but I have no choice, the xml files are like that, and I should work with many files like that..

Thanks in advance :)

Loic Mouchard
  • 1,121
  • 7
  • 22
Limmy
  • 53
  • 12

2 Answers2

0

I think you problem comes from your declaration of @XmlElement(name="Turn"). If you have a look to the following tutorial from Vogella, you'll see he is using a wrapper for its list thanks to the annotation XmlElementWrapper and sets the annotations at the declaration of the list, not before the method, as following:

// XmLElementWrapper generates a wrapper element around XML representation @XmlElementWrapper(name = "bookList")

// XmlElement sets the name of the entities @XmlElement(name = "book") private ArrayList bookList;

In your case, I think JAXB manage an object when you expect it to deal with a list of objects.

Community
  • 1
  • 1
Loic Mouchard
  • 1,121
  • 7
  • 22
  • JAXB manage a really list of objects, because, when i load my xml, getTurn() return a List of Turn like this for the previous example: 0:Turn with content equals: "un souci avec une inscription" 1:Turn with content equals:"ouais" 2:Turn with content equals:"second value!" Here the first value is missing.. – Limmy Jun 01 '16 at 14:01
  • OK, I first didn't get the point that the value of the Turn element is splited with your `Who` elements. Quite strange. Are you sure the definition of your input XML make sense? – Loic Mouchard Jun 01 '16 at 14:28
  • No, rather sure they are false, but they are not mine, i must work with it . – Limmy Jun 01 '16 at 14:45
0

So you deal with mixed content. Have a look to the other question how-to-deal-with-jaxb-complextype-with-mixedcontent-data and jaxb-xmlmixed-usage-for-reading-xmlvalue-and-xmlelement. It would be manageable with the annotation @XmlMixed for your Turn class. Then, I'm not quite sure if you need the getter and setter methods for the content.

Community
  • 1
  • 1
Loic Mouchard
  • 1,121
  • 7
  • 22
  • Nice ! i make a new attribute list with @XmlMixed, then it return all my content ! Thanks Loic M. – Limmy Jun 01 '16 at 15:07