How to parse xml string that contains the attribute on client using $.parseXML?

How to parse xml string that contains the attribute on client?

I have tried this code and it does not work:

$.parseXML('<foo xsi:type="SendAnswerViewModel"><bar1>Stuff</bar1><bar2>Stuff</bar2></foo>')

But this code works fine:

$.parseXML('<foo><bar1>Stuff</bar1><bar2>Stuff</bar2></foo>')

Upvotes: 0

Views: 111

Answers (1)

Quentin
Quentin

Reputation: 944015

Your XML is invalid. You can't use a namespace without declaring it.

If you add:

xmlns:xsi="http://example.com/"

(Use the correct namespace identifier though!)

Then it will parse successfully.

var x = $.parseXML('<foo xmlns:xsi="http://example.com" xsi:type="SendAnswerViewModel"><bar1>Stuff</bar1><bar2>Stuff</bar2></foo>');

alert($(x).find('bar1').text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 1

Related Questions