如何将xml文档对象转换为字符串? [英] how to convert xml document object to string?

查看:79
本文介绍了如何将xml文档对象转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串形式的 xml.我想更改其中的一些属性.
所以我所做的是

var xml//包含xml解析器 = 新的 DOMParser()xmlDoc = parser.parseFromString(xml, "text/xml")xmlDoc.setAttribute("名称", "随机")

现在 xmlDoc 具有所需的更改.但是如何再次获取xml的字符串表示呢?

解决方案

您需要在进行更改后将 xmlDoc 序列化回 XML:

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

现在你可以对更新后的 XML 字符串做你需要做的事情,覆盖你的 xml 变量,或者将它发送到服务器,或者其他任何......

请参阅 MDN 文档了解更多信息>

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?

解决方案

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

这篇关于如何将xml文档对象转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆