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.

3
  • 1
    can you please show us some examples of what kind of xml you are talking about? Commented Oct 17, 2011 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) Commented Oct 17, 2011 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. Commented Oct 18, 2011 at 9:40

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.