Unmarshalling XML JAXB with multiple elements same name
Solution 1
Maybe something like this:
@XmlRootElement(name="return")
public class returnBean {
private ArrayList<Row> rows;
public ArrayList<Row> getRows(){
return rows;
}
public void setRows(ArrayList<Row> rows){
this.rows = rows;
}
}
Notice the field is private now.
And then you probably don't need annotation here:
public class Row {
private String fkdevice;
public String getFkdevice() {
return fkdevice;
}
public void setFkdevice(String val) {
fkdevice = val;
}
}
Solution 2
Your field and method are both public. By default, JAXB binds every public field and every getter/setter pair.
One solution is to use @XmlAccessorType
to specify that fields and only fields are bound to XML.
@XmlRootElement(name="return")
@XmlAccessorType( XmlAccessType.FIELD )
public class testBean {
@XmlElement( name="fkdevice" )
public ArrayList<string> fkdevice;
...
}
laitha0
Updated on June 04, 2022Comments
-
laitha0 almost 2 years
I am trying to unmarshall an XML document that has multiple elements with the same name. I am not sure whether I need to create an Arraylist of my bean and pass it to the unmarshaller. I am hoping that somebody would give me some pointers to figure this out. The XML that I am trying to parse comes as a SOAP response but I stripped out the envelope so I only have teh body of it, it looks like this:
<return> <row> <fkdevice>bddc228e-4774-18b3-9c64-e218cbef7a8x</fkdevice> </row> <row> <fkdevice>74a5a260-bbd9-0491-7c58-0b1983180d2c</fkdevice> </row> <row> <fkdevice>312b5326-d7f1-4fb6-b1d9-dd96bb016152</fkdevice> </row> <row> <fkdevice>ed110481-e1e1-4659-ae09-1d23d888292b</fkdevice> </row> </return>
This is returned from a table that has more than 50 fields, but I created a testBean and I defined fkdevice only just to make it simple my bean looks like this:
package beans; //imports @XmlRootElement(name="return") public class testBean { //I think I need an arraylist here because I have multiple elements with teh same name. public ArrayList<string> fkdevice; public ArrayList<String> getFkdevice(){ return fkdevice; } public void setFkdevice(ArrayList<String> fkdevice){ this.fkdevice = fkdevice; } }
This gives me an error: 1 counts of IllegalAnnotationExceptions Class has two properties of the same name "fkdevice" and it points to the getter and setter.
Any info could be helpful, Thanks in advance