【问题标题】:Object to XML structure对象到 XML 结构
【发布时间】:2022-01-20 23:42:26
【问题描述】:

在我的专有应用程序中,我有一个具有以下结构的 XML 字段

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

我可以使用以下脚本写入它。

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

虽然上述方法有效,但我想知道为什么我不能使用以下方式。

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

我尝试了以下变体都无济于事,正确的方法是什么?

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

更新

为了能够扩展我的架构/表,我对我的架构进行了以下更改

<!--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 -->

因此我可以使用 XML xpath 将数据存储在单个 mData 字段(数据)中

var recipient = NLWS.nmsRecipient.create(
                   {recipient:{
                    email              : 'dummy@dummy.com', 
                    lastName           : 'D', 
                    firstName          : 'G',
                    origin             : 'Preference Centre'}})               

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

【问题讨论】:

  • changes.firstName 可能在构造函数中不存在,但在创建函数中的其他位置添加
  • 你可以在这里问github.com/adobe/acc-js-sdk

标签: javascript json xml


【解决方案1】:

我认为正确的方法应该是这样的。

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

区别在于构造对象的正确方法。

您的方式应该会引发语法错误,因为您尝试在创建对象之前访问 changes.firstName

【讨论】:

  • 这行得通,你能解释一下为什么行得通吗,顺便谢谢你,这像嵌套对象还是数组我不记得它叫什么了。
  • 它起作用了,因为这是创建具有嵌套值的对象的正确方法。您的错误是您甚至在创建 changes.firstName 之前就尝试访问它。
猜你喜欢
  • 2016-11-13
  • 1970-01-01
  • 1970-01-01
  • 2010-09-22
  • 1970-01-01
  • 1970-01-01
  • 2023-01-12
  • 2014-05-20
  • 1970-01-01
相关资源
最近更新 更多