How can i access the credit
sublist values in a for loop, from an xml string data. I am able to get to the //machine
but i couldnt get to the CREDIT lines.
my 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>
My client script code to get data:
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);
}
If you look at your xml you have two different machine nodes.
First step. Get the correct lines:
const lines = xml.XPath.select({
node: xmlDocument,
xpath: '//record/machine[@name="credit"]/line'
});
Then you have an array (not a NodeList) of line nodes.
For getting values I generally use a utility 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;
}
so
const credits = lines.map(function(line){
const lineRep = {
appliedTo: selectValue(line, 'appliedto'),
appliedToDoc: selectValue(line', 'doc'),
amount: parseFloat(selectValue(line, 'amount')) || 0,
...
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.