问题
I have already existing classes,I want to check whether there is any way to map the following XML into existing class.
Existing XML(jdom Element)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Details>
<Uniqueno>11111</Uniqueno>
<ROWSET name="Persons">
<ROW num="1">
<Name>60821894</Name>
<Age>938338789</Age>
</ROW>
<ROW num="2">
<Name>60821894</Name>
<Age>938338789</Age>
</ROW>
</ROWSET>
</Details>
Existing Class
@XmlRootElement(name = "Details")
@XmlAccessorType(XmlAccessType.FIELD)
class Details{
@XmlElement(name="Uniqueno")
String Uniqueno;
@XmlElement(name="ROWSET")
private Persons[] persons;
//setters & getters
}
@XmlRootElement(name = "Persons")
@XmlAccessorType(XmlAccessType.FIELD)
class Persons{
@XmlElement(name="name")
String name;
@XmlElement(name="age")
String age;
//setters & getters
}
The issue with which I am stuck is:I have a ROW num in XML which is not able to be mapped,Is any way to map the corresponding class without changing the structure of XML?
回答1:
This Code Would work
@XmlRootElement(name = "Details")
@XmlAccessorType(XmlAccessType.FIELD)
class Details
{
@XmlElement(name = "Uniqueno")
String Uniqueno;
@XmlElement(name = "ROWSET")
private Persons[] persons;
// setters & getters
}
@XmlRootElement(name = "ROWSET")
@XmlAccessorType(XmlAccessType.FIELD)
class Persons
{
@XmlAttribute
String name;
@XmlElement(name = "ROW")
private Row[] rows;
// setters & getters
}
@XmlRootElement(name = "ROW")
@XmlAccessorType(XmlAccessType.FIELD)
class Row
{
@XmlAttribute
String num;
@XmlElement
String Name;
@XmlElement
String Age;
// setters & getters
}
来源:https://stackoverflow.com/questions/10514017/issues-in-parsing-xml