XML files, as we know by now, are plain text files just like the HTML files. Hence, just like HTML, XML can also be easily stored and generated by a standard web server. For example, consider the following piece of code:
1 2 3 4 5 6 |
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <from>John</from> <to>Tove</to> <message>Remember me this weekend</message> </note> |
The above code would save the file on your web server with a proper name like “note.xml”.
As an alternative method, XML can be generated on a server without any installed XML software. To undertake this task, you need to take help from ASP. To generate an XML response from the server – simply write the following code and save it as an ASP file on the web server:
1 2 3 4 5 6 7 8 9 |
<% response.ContentType="text/xml" response.Write("<?xml version='1.0' encoding='ISO-8859-1'?>") response.Write("<note>") response.Write("<from>John</from>") response.Write("<to>Tove</to>") response.Write("<message>Remember me this weekend</message>") response.Write("</note>") %> |
The content type of the response must be set to “text/xml”. Finally, one can also generate an XML response file from the server using PHP. Utilization of PHP is perhaps the simpler way, considering the easy syntax of PHP and ease and simplicity of the language. The following piece of code would generate an XML response from the server using PHP:
1 2 3 4 5 6 7 8 9 |
<?php header("Content-type: text/xml"); echo "<?xml version='1.0' encoding='ISO-8859-1'?>"; echo "<note>"; echo "<from>John</from>"; echo "<to>Tove</to>"; echo "<message>Remember me this weekend</message>"; echo "</note>"; ?> |
Once again, the content type of the response header must be set to “text/xml”. Finally, we can also use ASP to save XML files on the server. For example, the following piece of ASP code creates a simple XML document and saves it on the server:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<% text="<note>" text=text & "<to>Tove</to>" text=text & "<from>John</from>" text=text & "<heading>Reminder</heading>" text=text & "<body>Don't forget me this weekend!</body>" text=text & "</note>" set xmlDoc=Server.CreateObject("Microsoft.XMLDOM") xmlDoc.async="false" xmlDoc.loadXML(text) xmlDoc.Save("test.xml") %> |
So basically, what we see is that it is easier to save XML files on the server utilizing ASP and PHP, particularly PHP. This shows the power of these web programming languages.