Jerome
Jerome

Reputation: 1192

How can I parse this string of xml?

I'm struggling to remove the <?xml version="1.0" encoding="UTF-8" standalone="no"?> of this string.

var string = "<?xml version='1.0' encoding='UTF-8' standalone='no'?><svg xmlns='http://www.w3.org/2000/svg...'"

console.log(string.split("?>")[1])

of course the string is bigger in reality but it's not useful to have it.

So how could I remove only the <?xml...> ?

[EDIT] I added a split but I'm not sure it's a professional way to remove the first tag

Upvotes: 0

Views: 55

Answers (3)

Priyesh Kumar
Priyesh Kumar

Reputation: 2857

Use regex and replace(requres less keystrokes *_* )

var string = "<?xml version='1.0' encoding='UTF-8' standalone='no'?><svg xmlns='http://www.w3.org/2000/svg...'"

console.log(string.replace(/<\?xml.*\?>/, ''))

Upvotes: 2

Baba Khedkar
Baba Khedkar

Reputation: 141

var string = "<?xml version='1.0' encoding='UTF-8' standalone='no'?><svg xmlns='http://www.w3.org/2000/svg...'";
var charIdx = str.indexOf('?>')+2;
string = string.substr(charIdx);

Upvotes: 1

Scott Marcus
Scott Marcus

Reputation: 65808

There are various ways to do this, but given that the part you want to exclude is static, this will do it:

var string = "<?xml version='1.0' encoding='UTF-8' standalone='no'?><svgxmlns='http://www.w3.org/2000/svg...'";

string = string.replace("<?xml version='1.0' encoding='UTF-8' standalone='no'?>","");

console.log(string)

Upvotes: 2

Related Questions