解析XML的问题

时间:2012-05-09 10:10:57

标签: java xml jaxb xml-parsing jdom

我已经有了现有的类,我想检查是否有任何方法可以将以下XML映射到现有的类中。

现有XML(jdom元素)

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

现有班级

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

我遇到的问题是:我在XML中有一个ROW num,无法映射,是否可以在不改变XML结构的情况下映射相应的类?

1 个答案:

答案 0 :(得分: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
}