creating a new, empty document with javascript
Solution 1
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));
Solution 2
You can create an empty document in a W3C DOM compliant browser (IE9+ and the rest) with the following code.
var doc = (new DOMParser()).parseFromString('<dummy/>', 'text/xml');
doc.removeChild(doc.documentElement);
Solution 3
I don't think you can create a document without the root node. You could create a fake node:
doc = (new DOMParser()).parseFromString("<dummy/>", 'text/xml');
However, a better solution might be to create constants for the node names:
// Use different variable names, like RealTxName, if desired
var REAL_TX_NAME = "TX";
var REAL_H_NAME = "H";
...
doc.find (REAL_TX_NAME);
...
Solution 4
var xmldom = require('xmldom');
var tree = (new xmldom.DOMImplementation()).createDocument();
Solution 5
The WHATWG's specification of the createDocument
method allows one to provide null
for each of the 3 arguments to createDocument
, to have an "empty" XML document created and returned -- one without any root node and with the application/xml
as its content type.
When the document
object is available (a compliant Web user agent), the implementation with the createDocument
method can be referred to with document.implementation
:
document.implementation.createDocument(null, null, null);
You may then trivially attach a root node of your choosing to it by simply appending it as you otherwise would, as follows (empty_doc
would refer to the document created and returned by the call above):
empty_doc.appendChild(empty_doc.createElement("foobar"));
morgancodes
Programmer/artist working on a monster javascript project for a cable television company for the past two and a half years. Meanwhile, I build magical sound experiences for iOS including Thicket, Morton Subotnick's Pitch Painter, and this fun toy payed for by gum. I also like to make geometric sculptures from paper. Future plans include releasing my C++ audio patching engine (build on top of STK) as an open source project, creating the world's most mesmerizing musical video game, building my own programming language, and finding a way to pay for it all.
Updated on June 23, 2022Comments
-
morgancodes almost 2 years
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?
I've tried this:
doc = (new DOMParser()).parseFromString("", 'text/xml');
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?
-
morgancodes almost 15 yearsThanks. I've thought about that approach to, and may wind up using it. I'm just taking a few steps in several directions to see which way looks/feels more promising.