TransWikia.com

Parsing XML values in Javascript

Stack Overflow Asked by topcat3 on December 30, 2021

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'));

2 Answers

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

Answered by topcat3 on December 30, 2021

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);

Answered by Keith on December 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP