-1

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 .

2

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.