简体   繁体   中英

How to Handle this XML in JAXB with annotations?

I have 2 questions on how how to parse a specific aspect of this XML in JAXB with annotations.

Assuming I have a FosterHome class defined...

1) For the "Children" tag, is there a way to contain the properties within a "Children" class without having to create a separate "Info" class?

2) Since the perperties of the "Info" tag are related to "ChildList" also, should I be having two Child classes one without Info and one with Info? Or is there a better way?

Im trying to find the best way to parse this. Please refer to this posting for current set of classes: How do I parse this XML in Java with JAXB?

<FosterHome>
<Orphanage>Happy Days Daycare</Orphanage>
<Location>Apple Street</Location>
<Families>
    <Family>
        <ParentID>Adams</ParentID>
        <ChildList>
            <ChildID>Child1</ChildID>
            <ChildID>Child2</ChildID>
        </ChildList>
    </Family>
    <Family>
        <ParentID>Adams</ParentID>
        <ChildList>
            <ChildID>Child3</ChildID>
            <ChildID>Child4</ChildID>
        </ChildList>
    </Family>
</Families>
<Children>
    <ChildID>Child1</ChildID>
    <Info>
       <Age>6</Age>
       <FavColor>Blue</FavColor>
    </Info>
    <ChildID>Child2</ChildID>
    <Info>
       <Age>8</Age>
       <FavColor>Red</FavColor>
    </Info>
    ...
</Children>
</FosterHome>

To answer #1, you might want to use MOXy . With that, you can use @XPath to get the age and favColor . In fact, @Blaise Doughan is the person you might want to talk to (he answered your previous post) because he's the team lead for that MOXy project.... pretty active in this forum too. Here's an example of the usage similar to yours: With MOXy and XPath, is it possible to unmarshal a list of attributes?

As for #2, your XML structure looks weird. If you are allowed to change the XML, I would add the the child age and favorite color together with the child name, something like this:-

...
<Family>
    <ParentID>Adams</ParentID>
    <ChildList>
        <ChildID name="Child1" age="6" favColor="Blue"/>
        <ChildID name="Child2" age="8" favColor="Red"/>
    </ChildList>
</Family>
...

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