views:

337

answers:

2

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?

+1  A: 

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);
...
John Millikin
Thanks. 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.
morgancodes
A: 

Hi,

I hade the same issue and I solvded it like below:

xmlDoc = document.implementation.createDocument("","",null); root = xmlDoc.createElement("description"); xmlDoc.appendChild(root); alert((new XMLSerializer()).serializeToString(xmlDoc));

Cheers, Ralph

Ralph