Tuesday, 9 August 2011

Creating XML Structure


XML and DOM are everywhere. In this chapter, you used the DOM to create HTML elements on the existing DOM object called document, and you also learned how to read XML documents received from the server. An important detail that we didn't cover was creating brand new XML documents using JavaScript's DOM. You may need to perform this kind of functionality if you want to create XML documents on the client, and send them for reading on the server. We won't go through more examples, but we will only show you the missing bits. The trick with creating a brand new XML document is creating the XML document itself. When adding elements to the HTML output, you used the implicit document object, but this is not an option when you need to create a new document. When creating a new DOM object with JavaScript, we're facing the same problem as with creating XMLHttpRequest objects; the method of creating the object depends on the browser. The following function is a universal function that returns a new instance of a DOM object: function createDomObject() { // will store reference to the DOM object var xmlDoc; // create XML document if (document.implementation && document.implementation.createDocument) { xmlDoc = document.implementation.createDocument("", "", null); } // works for Internet Explorer else if (window.ActiveXObject) { xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); } // returns the created object or displays an error message if (!xmlDoc) alert("Error creating the DOM object."); else return xmlDoc; } After executing this function, you can use the created DOM object to perform whatever actions you want. For more details about creating the DOM object check the following link:http://www.webreference.com/programming/javascript/domwrapper/index.html. For details of using the DOM object, refer to the DOM articles mentioned earlier in this chapter.

No comments:

Post a Comment

                                     Home                      Vector Graphics           Our Works           Contact Me
                                              Maintained By: Sarath S Pillai, E-mail: sarathparavur007@gmail.com