1

I want to copy/clone 2 xml dom objects - one into another.

Sorry if it is duplicate question... I've already tried What is the most efficient way to deep clone an object in JavaScript?

But jquery throws Illegal invocation

EDIT: XML comes from an ajax call

function FetchXMLData() {
                $.ajax({
                    url : "resources/data.xml",
                    data : requestVars,
                    dataType : 'xml',
                    success : function(xml) {
                        XMLParser(xml);
                        xmlOrig = $(xml).clone(); // I want to do something like this
                    },
                    error : function(xhr, err) {
                        ShowErrors(xhr, err);
                    }
                });
            }

EDIT 2: Now, I'm using javascript cloneNode, that works perfectly in IE and FF, but fails in Chrome. See this. If anyone knows a workaround I would be grateful.

Community
  • 1
  • 1
jose
  • 2,733
  • 4
  • 37
  • 51
  • 1
    can you please show us some examples of what kind of xml you are talking about? – Andreas Louv Oct 17 '11 at 11:54
  • 1
    What does `XMLParser` do? Don't you have to assign what ever is returned from that method to a variable? `xml = XMLParser(xml)` – peirix Oct 17 '11 at 13:55
  • xmlParser just fetches some useful information from the xml file. I was trying to clone/replicate that xmlin order to keep the original structure. – jose Oct 18 '11 at 09:40

1 Answers1

1

I know it's an old question, but i just had this same problem, and solved it converting it to text and again to XML.

Having:

(...)
success : function(xml) {
    XMLParser(xml);
    xmlOrig = cloneXML(xml);
},
(...)

// auxiliar function to clone XML
function cloneXML(xml) {
    var xml_text = (new XMLSerializer()).serializeToString(xml);
    return $.parseXML(xml_text); // return XML document
}

This workaround worked for me in Firefox 49.0.2 and Chrome 54.0.2840.71.

If anyone know a better way, please answer.

eventHandler
  • 1,088
  • 12
  • 20