用javascript创建一个新的空文件 [英] creating a new, empty document with javascript
问题描述
我正在使用一些非常不直观的xml(所有的标签都是TX,H,VC)。
I'm working with some very unintuitive xml (all the tags are things like "TX", "H", "VC").
我想制作这些数据的副本,但所有的标签都重命名为实际的意思。我可以创建一个新的,空的文档来放入我的新的,很好的命名标签吗?
I'd like to make a copy of this data, but with all of the tags renamed to what they actually mean. Can I create a new, empty document to put my new, nicely named tags in to?
我已经尝试过:
doc = (new DOMParser()).parseFromString("", 'text/xml');
但是当我这样做的时候,我结束了一个有一个子节点的文档,而不是空。此外,该小孩的标记名是parsererror
but when I do so, I wind up with a document that has a child node, rather than being empty. Furthermore, that child's tagname is "parsererror"
所以,任何想法如何创建一个空的文档?
So, any ideas how I can create an empty document?
推荐答案
我讨论同样的问题,我解决了如下:
I hade the same issue and I solved it like below:
xmlDoc = document.implementation.createDocument("", "", null);
root = xmlDoc.createElement("description");
xmlDoc.appendChild(root);
alert((new XMLSerializer()).serializeToString(xmlDoc));
这篇关于用javascript创建一个新的空文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!