Home > Mobile >  Object to XML structure
Object to XML structure

Time:12-18

On my proprietary application I have an XML field with the following structure

<?xml version='1.0'?>
<recipient>
    <comment></comment>
    <changes firstName=""/>
</recipient>

I can write into it using the following script.

var recipient = NLWS.nmsRecipient.create(
                   {recipient:{
                    email              : '[email protected]', 
                    lastName           : 'D', 
                    firstName          : 'G',
                    origin             : 'Preference Centre'}})               
                    
                    recipient.changes.firstName = 'tmpName'
                    recipient.comment = "CommentsHere";                 
                    recipient.save(); 

While the above works, I am wondering why I can't use the following way.

var recipient = NLWS.nmsRecipient.create(
                   {recipient:{
                    email              : '[email protected]', 
                    lastName           : 'D', 
                    firstName          : 'G',
                    origin             : 'Preference Centre'
                    changes.firstName  : 'tmpFirstName'}})     
                              
                    recipient.comment = "CommentsHere";                 
                    recipient.save(); 

I've tried the following variations to no avail, what would be the correct method?

[changes/@firstName]  : 'tmpFirstName'
[changes.firstName]  : 'tmpFirstName'
{changes.firstName}  : 'tmpFirstName'
{"changes":"firstName"}  : 'tmpFirstName'

records

Update

For my to be able to extend my schema/table, I made the following change on my schema

<!--tmp recipient changes 18122021 DG-->
  <element label="changes" name="changes" xml="true">
    <attribute label="tmp firstName" name="firstName" type="string" xml="true"/>
    <attribute label="tmp LastName" name="lastName" type="string" xml="true"/>
    <attribute label="tmp email" name="email" type="string" xml="true"/>
    <attribute label="tmp emailPreferredName" name="emailPreferredName" type="string"
               xml="true"/>
    <attribute label="tmp JOB_TITLE" name="JOB_TITLE" type="string" xml="true"/>
    <attribute label="tmp company" name="company" type="string" xml="true"/>
    <attribute label="tmp blackListEmail" name="blackListEmail" type="string"
               xml="true"/>
    <attribute label="tmp lawfulBasis" name="lawfulBasis" type="string" xml="true"/>
  </element>
<!--tmp recipient changes -->

and I can therefore store data in the single mData field (data) using XML xpath

var recipient = NLWS.nmsRecipient.create(
                   {recipient:{
                    email              : '[email protected]', 
                    lastName           : 'D', 
                    firstName          : 'G',
                    origin             : 'Preference Centre'}})               

                    recipient.changes.firstName = 'tmpName'
                    recipient.changes.lastName= 'tmpName'
                    recipient.changes.company= 'tmpCompany'
                    recipient.comment = "CommentsHere";                 
                    recipient.save(); 

CodePudding user response:

I think the correct way should be something like that.

var recipient = NLWS.nmsRecipient.create(
                   {recipient:{
                    email              : '[email protected]', 
                    lastName           : 'D', 
                    firstName          : 'G',
                    origin             : 'Preference Centre',
                    changes: {
                        firstName : 'tmpFirstName'
                    }
                }})     
                              
                    recipient.comment = "CommentsHere";                 
                    recipient.save(); 

The difference is the correct way of structuring an object.

Your way should throw a syntax error because you try to access the changes.firstName before the object gets created

  • Related