3

I have xml that I have in a string. I want to change some attributes in it.
So what I have done is

var xml //contains xml  
parser = new DOMParser()
xmlDoc = parser.parseFromString(xml, "text/xml")
xmlDoc.setAttribute("name", "random")

Now the xmlDoc has the required changes. But how do I get the string representation of the xml again?

1 Answer 1

6

You will need to serialize your xmlDoc back to XML once you have made the changes:

var s = new XMLSerializer();
var newXmlStr = s.serializeToString(xmlDoc);

Now you can do what you need to do with the string of updated XML, overwrite your xml variable, or send it to the server, or whatever...

See the MDN docs for further info

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

1 Comment

Works. Thanks. Much appreciated. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.