aldr
aldr

Reputation: 848

JAXB xsd how to use members of an array like a reference

I have two well formed .xml Input Files containing data.

character.xml

    <Character ID="1">
        <Name>jfkfd</Name>
        <CharPart>
            <Marks>mk1 mk4 mk6 mk9</Marks>
    ...

and

marks.xml

    <Marks>
    <Type ID="mk0">
            <Name>None</Name>
    </Type>
    <Type ID="mk1">
            <Name>abc</Name>
    </Type>
    <Type ID="mk2">
            <Name>def</Name>
    </Type>
    ...

I created a schema.xsd definig both character and marks. So far so good.

<xsd:element name="Character">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="Name" type="xsd:string" />
            <xsd:element name="CharPart">
                <xsd:complexType>
                    <xsd:sequence>                        
                        <xsd:element name="Marks" type="marksList"/>                               
                    </xsd:sequence>
                </xsd:complexType>                    
            </xsd:element>                                          
        </xsd:sequence>
        <xsd:attribute name="ID" type="xsd:string"/>
    </xsd:complexType>
</xsd:element>

<xsd:element name="Marks">        
    <xsd:complexType >
        <xsd:sequence>
            <xsd:element name="Type" maxOccurs="unbounded">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="Name" type="xsd:string" />
                    </xsd:sequence>
                    <xsd:attribute name="ID" type="xsd:string" />
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>   

<xsd:simpleType name="marksList">
    <xsd:list itemType="xsd:string"/>
</xsd:simpleType>  

Unmarschalling both files enables me to Iterate through both lists. This way I'm checking for equality between values from marksList and id like so:

    for(Marks.Type type : marks.getType()) {
        for(String s : character.getCharPart().getMarks()){
            if(s.equals(type.getID())){
            ... 
            }
        }
    }

My question now is there a way how I can use <Marks>mk1 mk4 mk6 mk9</Marks> without having to use a preprocessor splitting them up, so that JAXB automatically creates Java sources treating these "String" values as references?

Upvotes: 1

Views: 184

Answers (1)

lexicore
lexicore

Reputation: 43671

Try changing type to xsd:IDREF or generally use xsd:IDREFS instead of marksList.

Upvotes: 1

Related Questions