2

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>

1 Answer 1

1

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>
...
2
  • +1 - Here is a link to some additional MOXy @XmlPath examples: blog.bdoughan.com/search/label/XmlPath
    – bdoughan
    Commented Nov 19, 2011 at 12:18
  • I am unable to change the XML structure. The link looks like XmlPath takes contents from multiple files. How can it be used to apply to my example with a single XML?
    – Rolando
    Commented Nov 20, 2011 at 23:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.