Issues in Parsing XML

空扰寡人 提交于 2019-12-13 02:45:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!