簡體   English   中英

如何從 SuiteScript 2.0 中的 XML 數據中獲取值? 網絡套件

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM