[英]How to get values from XML data in SuiteScript 2.0? NetSuite
如何从 xml 字符串数据访问 for 循环中的credit
子列表值。 我能够到达//machine
但无法到达 CREDIT 行。
我的 XML:
<!--?xml version="1.0" encoding="UTF-8"?-->
<nsresponse>
<record
fields="_eml_nkey_,_multibtnstate_,selectedtab,nsapiPI,nsapiSR,nsapiVF,nsapiFC,nsapiPS,nsapiVI,nsapiVD,nsapiPD,nsapiVL,nsapiRC,nsapiLI,nsapiLC,nsapiCT,nsbrowserenv,wfPI,wfSR,wfVF,wfFC,wfPS,type,id,externalid,whence,customwhence,entryformquerystring,_csrf,wfinstances,dbstrantype,bulk,createddate,lastmodifieddate,periodclosed,allownonglchanges,taxperiod,version,voided,linked,linkedrevrecje,linkedclosedperioddiscounts,entityname,status,statusRef,trantypepermcheck,ntype,nluser,nlrole,nldept,nlloc,nlsub,baserecordtype,nlapiCC,nexus,warnnexuschange,nexus_country,entitynexus,extraurlparams,taxamountoverride,taxamount2override,emailaddr,transactionnumber,entity,subsidiary,account,balance,total,currency,exchangerate,trandate,voidjournal,postingperiod,tobeprinted,printvoucher,tranid,apacct,memo,department,class,location,custbody_9997_is_for_ep_eft,custbody_11724_pay_bank_fees,custbody_ven_arabic_name,custbody_payment_proff,custbody_payment_supplierstatement,custbody12,nextaccountdocnum,accttype,entityfieldname,currencyname,currencysymbol,currencyprecision,isbasecurrency,origexchangerate,origcurrency,address,payeeaddress_key,payeeaddress,recordcreatedby,recordcreateddate,prevdate,ppsetbyuser,ppsetbyuservalue,pp_s,pp_e,nexttranid,initialaccount,origtotal,custbody_11187_pref_ebd_details,custbody_9997_pfa_record,custbody_11187_pref_entity_bank,custbody_11724_bank_fee,custbody_15529_vendor_entity_bank,custbody_15529_emp_entity_bank,custbody_4599_mx_operation_type,transactionnumber,custbody_4601_total_amt,custbody_wtax_base_url,custbody_4601_wtax_withheld,custbody14,cleared,cleareddate,companyid"
id="6270186" perm="4" recordtype="vendorpayment">
<machine fields="apply,doc,applydate,type,internalid,trantype,refnum,total,due,currency,discdate,discamt,disc,userenteredamount,userentereddiscount,amount,line,pymt,Transaction_CLASS,Transaction_LOCATION,Transaction_MEMO,CUSTBODY_BILL_ODOO_REFNO,CUSTBODY7,createdfrom,duedate" name="apply" type="list">
<line>
<amount>2536.23
</amount>
<apply>T
</apply>
<applydate>30/08/2022
</applydate>
<currency>SAR
</currency>
<doc>5079021
</doc>
<due>2536.23
</due>
<duedate>30/08/2022
</duedate>
<internalid>5079021
</internalid>
</userentereddiscount>
</line>
</machine>
<machine fields="apply,doc,creditdate,type,internalid,trantype,refnum,doc2,appliedto,currency,amount,line,pymt,createdfrom,duedate" name="credit" type="list">
<line>
<amount>7240.27
</amount>
<appliedto>Bill #2210147393
</appliedto>
<apply>T
</apply>
<creditdate>31/08/2022
</creditdate>
<currency>SAR
</currency>
<doc>6001714
</doc>
<doc2>4566846
</doc2>
<internalid>6001714
</internalid>
<line>0
</line>
<pymt>6270186
</pymt>
<trantype>VendCred
</trantype>
<type>Bill Credit
</type>
</line>
<line>
<amount>961.41
</amount>
<appliedto>Bill #2210147393
</appliedto>
<apply>T
</apply>
<creditdate>31/08/2022
</creditdate>
<currency>SAR
</currency>
<doc>6117411
</doc>
<doc2>4566846
</doc2>
<internalid>6117411
</internalid>
<line>0
</line>
<pymt>6270186
</pymt>
<trantype>VendCred
</trantype>
<type>Bill Credit
</type>
</line>
<line>
<amount>51843.82
</amount>
<appliedto>Bill #2110245041
</appliedto>
<apply>T
</apply>
<creditdate>31/08/2022
</creditdate>
<currency>SAR
</currency>
<doc>6117411
</doc>
<doc2>4960897
</doc2>
<internalid>6117411
</internalid>
<line>0
</line>
<pymt>6270186
</pymt>
<trantype>VendCred
</trantype>
<type>Bill Credit
</type>
</line>
</machine>
</baserecordtype>
</record>
</nsresponse>
我的客户端脚本代码来获取数据:
var xmlDocument = xml.Parser.fromString({
text: xmlString
});
var dataNode = xml.XPath.select({
node: xmlDocument,
xpath: '//machine'
});
for (var i = 0; i < dataNode.length; i++) {
log.debug('Config content', dataNode[i].textContent);
}
如果您查看 xml,您有两个不同的机器节点。
第一步。 获取正确的行:
const lines = xml.XPath.select({
node: xmlDocument,
xpath: '//record/machine[@name="credit"]/line'
});
然后你有一个线节点的数组(不是 NodeList)。
为了获取值,我通常使用实用程序 function
function selectValue(node, path) {
const targets = xml.XPath.select({
node: node,
xpath: path
});
if (!targets || !targets.length) return null;
return targets[0].firstChild.nodeValue;
}
所以
const credits = lines.map(function(line){
const lineRep = {
appliedTo: selectValue(line, 'appliedto'),
appliedToDoc: selectValue(line', 'doc'),
amount: parseFloat(selectValue(line, 'amount')) || 0,
...
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.