views:

206

answers:

1
+1  Q: 

XML in JavaScript

I have an XML Schema which I am parsing in JavaScript and then packing that as an object to pass to one of my backend servers.

The schema is like

<complexType name='Test'>
    <sequence>  
        <element name='testField' type='string'/>
        <element name='typeSpecificSetting' type='tns:TypeSpecific'/>
    </sequence> 
</complexType>
<complexType name="TypeSpecific">
    <choice>   
         <element name='A' type='tns:ATYPE'/>
         <element name='B' type='tns:BTYPE'/>
         <element name='C' type='tns:CTYPE'/>
         <element name='D' type='tns:DTYPE'/>
    </choice>
</complexType>

<complexType name="ATYPE">
    <element name='testATYPEField' type='string'/>
</complexType>

<complexType name="BTYPE">
     <element name='testBTYPEField' type='string'/>
</complexType>

I am reading the xml schema and then trying to construct my request object.

request = { 
    testField:  t1,
    typeSpecificSetting: t2
}

How can I construct the request object for choice? Depending upon type, I have to pack either of ATYPE or BTYPE or CTYPE or DTYPE objects? How can I achieve this?

+1  A: 

Since typeSpecific is a complex type with just one choice then typeSpecificSetting property will be an object that contains one property which will one of testATYPEField, testBTYPEField, ...

 request = {
     testField: t1
     typeSpecificSetting: {
         A: { 
             testATYPEField: t2
         }
     }
 }

OR

 request = {
     testField: t1
     typeSpecificSetting: {
         B: { 
             testBTYPEField: t2
         }
     }
 }

etc.

AnthonyWJones