JaxB包含复杂类型的列表

时间:2016-12-10 01:41:45

标签: java xml jaxb

我在摆弄jaxb,想知道是否有人能告诉我我做错了什么:

类:

@XmlRootElement(name = "zoo")
@XmlAccessorType(XmlAccessType.FIELD)
public class zoo()
{
    @XmlElementWrapper(name = "CATS")
    private List<CatSnapshot> cats;
    // other stuff
}

@XmlAccessorType(XmlAccessType.FIELD)
public class CatSnapshot
{
    @XmlElement
    private Cat cat;
    // some other stuff
}

@XmlAccessorType(XmlAccessType.FIELD)
public class Cat()
{
    // cat stuff
}

Xml结果:

<CATS>
    <cats>
        <cat> ... </cat>
    </cats>
    <cats>
        <cat> ... </cat>
    </cats>
    <cats>
        <cat> ... </cat>
    </cats>
</CATS>

现在,困扰我的是每只猫被猫包围,我想要像:

<CATS>
<cat> ... </cat>
<cat> ... </cat>
<cat> ... </cat>
</CATS>

我做错了什么?

谢谢

1 个答案:

答案 0 :(得分:0)

您可以将Zoo与library(dplyr) Data %>% group_by(Project) %>% mutate( fl = as.numeric(NewValue), flag = paste(lag(fl, 2, default = 0), lag(fl, 1, default = 0), fl, sep = ''), merge = paste(lag(Value, 2, default = 0), lag(Value, 1, default = 0), Value, sep = ',') ) %>% filter(flag == '321' | flag == '324') %>% select(Project, merge) # Project merge # <dbl> <chr> # 1 123 1,4,7 # 2 123 3,8,9 # 3 124 8,3,2 # 4 125 2,2,1 一起使用。看起来像是:

List<Cat>

结果是:

@XmlRootElement(name = "zoo")
@XmlAccessorType(XmlAccessType.FIELD)
public class Zoo
{
    @XmlElementWrapper(name = "CATS")
    private List<Cat> cat = new ArrayList<Cat>();
    // other stuff

    public static void main(String[] args) throws JAXBException {
        Cat cat1 = new Cat();
        Cat cat2 = new Cat();
        Cat cat3 = new Cat();

        Zoo zoo = new Zoo();
        zoo.cat.add(cat1);
        zoo.cat.add(cat2);
        zoo.cat.add(cat3);

        System.out.println(zoo);
        JAXBContext jc = JAXBContext.newInstance(Zoo.class);
        Marshaller marshaller1 = jc.createMarshaller();
        marshaller1.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller1.marshal(zoo, System.out);

    }
}

希望得到这个帮助。