简体   繁体   中英

Completly new to XML and JAXB. How do I go about unmarshalling this snippet?

This is a sample of the XML I've got. It contains several more rows with item

<items>
    <item name="Alfa Romeo">
        <models>
            <model to="2001" since="1994" name="145/146" />
            <model since="2001" name="147" />
            <model to="1997" since="1992" name="155" />
            <model since="1997" name="156" />
            <model since="2007" name="159" />
            <model to="1998" since="1990" name="164" />
            <model since="1999" name="166" />
            <model since="2004" name="GT" />
            <model since="1996" name="GTV" />
            <model to="2007" since="1996" name="Spider" />
            <model since="2007" name="Spider" />
        </models>
    </item>
    <item name="Fiat">
        <models>
            <model since="1995" name="Barchetta" />
            <model since="2007" name="Bravo" />
            <model since="1995" name="Bravo and Brava" />
            <model to="1998" since="1993" name="Cinquecento" />
            <model to="2000" since="1995" name="Coupe" />
            <model since="2001" name="Doblo" />
            <model since="2007" name="Grande Punto" />
            <model to="2007" since="2004" name="Idea" />
            <model to="2001" since="1997" name="Marea" />
            <model since="1999" name="Multipla" />
            <model since="2004" name="Panda" />
            <model to="2003" since="1994" name="Punto" />
            <model since="2003" name="Punto" />
            <model to="2004" since="1998" name="Seicento" />
            <model since="2002" name="Stilo" />
            <model to="1996" since="1991" name="Tempra" />
            <model to="1996" since="1989" name="Tipo" />
            <model to="2003" since="1995" name="Ulysse" />
        </models>
    </item>
</items>

Is it posible to unmarshall this into objects of Model with fields private int to; private int since; private String name; contained in an ArrayList inside models of Car(item) contained in an ArrayList?

Alternatively with objects of Car with fields private String brand(name); private int since; private int to; private String model(name) contained in an ArrayList?

If so how? or if not how other way can I retrieve the information into objects and a data structure?

I've started going through the very basic tutorials to learning JAXB, so I'm getting there step by step, but I would really like to be able to use this XML data in another java project I'm working on as soon as possible.

Thanks.

You should start by generating you POJO's. You can use an online tool to do this if you haven't done this already, see How to generate JAXB classes from just XML

I'm not sure what you're trying to accomplish with "private" explanation. First step, get it to unmarshall. You can then change decorations to make appropriate private fields - see How to marshal/unmarshal Java objects with private fields using JAXB

Essentially, use @XmlElement on private members:

public class model {
  @XmlAttribute
  private String to;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM