I am trying to get values from an XML string which is like this
<ROOT>
<ADDALTERNATE>
<DATA>
<SELECTEDDATA ID="3534" NAME="sampleName" CODE="SampleCode" CODEIDS="133277,133278,133279,133280"/>
</DATA>
</ADDALTERNATE>
</ROOT>
How do I get the values from CODE tag from the given XML in javascript simplest way? tried different way it did not work .
You can use the DOMParser combined with a little XPATH to locate the required value.
const xmlStr = `<ROOT>
<ADDALTERNATE>
<DATA>
<SELECTEDDATA ID="3534" NAME="sampleName" CODE="SampleCode" CODEIDS="133277,133278,133279,133280"/>
</DATA>
</ADDALTERNATE>
</ROOT>`;
const parser = new DOMParser();
const doc = parser.parseFromString(xmlStr, "application/xml");
const code = doc.evaluate(`//SELECTEDDATA/@CODE`, doc, null, XPathResult.STRING_TYPE, null);
console.log(code.stringValue);