最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to convert xml document object to string? - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked Jun 29, 2017 at 10:19 Jeff P ChackoJeff P Chacko 5,0185 gold badges25 silver badges35 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

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

发布评论

评论列表(0)

  1. 暂无评论