Writing XML files using jQuery

chaning xml attributes via jquery is easy, simple, simple:

$(this).attr('name', 'hello'); 

but how can I add another tag to the file? I tried using silent js.

Is there any way to do this?

Explanations: This code is part of the extension for firefox, so do not worry about saving to the user's file system. Still append does not work for XML documents, but I can change the xml attribute values

+4
source share
2 answers

The problem is that jQuery creates a new node in the current web page document , so as a result, node cannot be added to another XML document. Thus, node must be created in an XML document.

You can do it like this:

 var xml = $('<?xml version="1.0"?><foo><bar></bar><bar></bar></foo>'); // Your xml var xmlCont = $('<xml>'); // You create a XML container xmlCont.append(xml); // You append your XML to the Container created in the main document // Now you can append without problems to you xml xmlCont.find('foo bar:first').append('<div />'); xmlCont.find('foo bar div'); // Test so you can see it works 
+10
source

I suggest you go through the code with a debugger and see if you can determine why the append is causing the error (or if there is any error somewhere else). Sort of:

 $('selector').append('<p></p>'); 

should work fine.

+1
source

All Articles