0

The browser is IE8 and I would just like to resolve the getValueCovid function to retrieve ACCT_NO. The other getValueFromStructure is working.

getValueCovid has the full xml response as response.

I have the below XML and I already have a javascript function that gets the elements from structure. What I want to do is write a seperate javascript function to retrieve ACCT_NO and values from the 'Fields' level. Code below

<Response>
    <Cl586>
        <Fields>
            <Field name="ACCT_NO">
                <Value>12345</Value>
            </Field>
            <Structure id="2002" index="1">
                <Field name="ACCT_STATUS">
                    <Value>TEST</Value>
                </Field>
            </Structure>
        </Fields>
    </Cl586>
</Response>

Working javascript for structure getting by index:

getValueFromStructure : function (structure, name) {
    if (structure !== null && structure.hasOwnProperty('fields')) {
        for (var i=0; i < structure.fields.length; i++) {
            if (structure.fields[i].name === name) {
                return structure.fields[i].value;
            }
        }
    }
    return null;
},

My attempt to get ACCT_NO which I want to fix

getValueCovid : function(response, name) {
    if (response !== null && response.hasOwnProperty('fields')) {
        for (var i=0; i < response.fields.length; i++) {
            if (response.fields[i].name === name) {
                return response.fields[i].value;
            }
        }
    }
    return null;
},

Then in seperate file I want to retrieve ACCT_NO. Including so you have more idea:

$('#accountNumber').val(ClRestServices.getValueCovid(response, 'ACCT_NO'));
4
  • In what environment are you executing this? If from a browser, why not parse it as a DOM tree and use DOM methods? jsfiddle.net/g3w8d2tf
    – Kaiido
    Commented Jul 24, 2020 at 14:38
  • @Kaiido its in IE8
    – topcat3
    Commented Jul 24, 2020 at 14:41
  • 1
    Please edit your question with all requirements and limitations. Commented Jul 24, 2020 at 14:56
  • 2
    IE8 oO that should definitely be part of the first things in your question. Developping for IE8 is not compatible with what is called today javascript, you have to go back on every metgpd to double check for a quirk, and most programmers nowadays never had to even deal with that (lucky them). Now I think I remember IE had other means to parse XML even before they add the DOMParser interface, was it in ActiveXObject? I don't remember very well but I guess it's easily findable, but I don't have time rn.
    – Kaiido
    Commented Jul 24, 2020 at 15:03

2 Answers 2

0

If you parse into a standard DOM node, you can use querySelector etc.

eg.

const text = `<Response>
    <Cl586>
        <Fields>
            <Field name="ACCT_NO">
                <Value>12345</Value>
            </Field>
            <Structure id="2002" index="1">
                <Field name="ACCT_STATUS">
                    <Value>TEST</Value>
                </Field>
            </Structure>
        </Fields>
    </Cl586>
</Response>`;


const parser = new DOMParser();
const xmlDoc = parser.parseFromString(text,"text/xml");

const value = xmlDoc.querySelector('Field[name=ACCT_NO] Value').textContent;


console.log(value);

2
  • thanks I tried but not working for me. I've reedited my question with the requirement. thanks for your help
    – topcat3
    Commented Jul 24, 2020 at 15:03
  • IE8, is a very old unsupported browser, but if you need support for it, usually the best option is to see if there are pollyfills. Eg. npmjs.com/package/xmldom
    – Keith
    Commented Jul 24, 2020 at 19:30
0
getValueCovid : function(response, flag) {
    var root = response.documentElement;
      for (var i = 0, l = response.fields.fields.length; i < l; i++) {
        var input = response.fields.fields[i];
        if (input.name == flag) {
              return input.value;
        }
        if (input.name1) {
          var xmlElement = root.querySelector(input.name1);
          if (xmlElement) {
            input.value = xmlElement.textContent;
          }
        }
      }
},

This worked for me

Not the answer you're looking for? Browse other questions tagged or ask your own question.