Despite with all the new web technologies around us, I guess that XML is something that wouldn't die out so easily. Especially that its a friendly file type that can be opened by different browsers and softwares. Ex: Mozilla Firefox, Google Chrome, Microsoft Excel, etc... Therefore I'm going to share an example using php that would display the results as a xml data.
Here's a simple php file that contains the necessary functions.
<?php //Header needed to display content as xml header ("Content-Type:text/xml"); //function to create xml node //if $hasIllegalCharacters is true, then we will use CDATA to wrap the node values function createNode($nameOfNode, $valueOfNode, $hasIllegalCharacters=true) { $result = ""; $result .= "<" . $nameOfNode . ">"; if($hasIllegalCharacters) { $result .= "<![CDATA["; } $result .= $valueOfNode; if($hasIllegalCharacters) { $result .= "]]>"; } $result .= "</" . $nameOfNode . ">"; return $result; } //function to save result as xml file function createAttachment($filename) { header('content-disposition: attachment; filename='.$filename.'.xml'); } ?>
The following would be the source codes of the example
<?php //include the xml custon library include("php/library/xml/XmlMainClass.php"); //Create the nodes and populate the nodes with data $message = createNode("message", 'Meow !@#$%^&*()-=_+{}[]\|:"'.";'<>?,./`~"); $lat = createNode("lat", "1.281270"); $lng = createNode("lng", "103.825318"); $coordinates = createNode("coordinates", $lat . $lng, false); $country = createNode("country", "Singapore"); $street = createNode("street", "Jalan Membina"); $address = createNode("address", $street . $country . $coordinates, false); $dob = createNode("dob", "></></><"); $age = createNode("age", "28++"); $name = createNode("name", "nekyouto"); $profile = createNode("profile", $name.$age.$dob.$address.$message, false); //Output the results echo $profile; ?>
A similar example to the previous one but this time round rather than displaying the results, it will create a downloadable xml file.
<?php //include the xml custon library include("php/library/xml/XmlMainClass.php"); //Create the nodes and populate the nodes with data $message = createNode("message", "Meow !@#$%^&*()-=_+{}[]\|:\";'<>?,./`~"); $lat = createNode("lat", "1.281270"); $lng = createNode("lng", "103.825318"); $coordinates = createNode("coordinates", $lat . $lng, false); $country = createNode("country", "Singapore"); $street = createNode("street", "Jalan Membina"); $address = createNode("address", $street . $country . $coordinates, false); $dob = createNode("dob", "></></><"); $age = createNode("age", "28++"); $name = createNode("name", "nekyouto"); $profile = createNode("profile", $name.$age.$dob.$address.$message, false); createAttachment("profile"); //Output the results echo $profile; ?>
* Click here to view the demo of the first example.
^ Click here to view the demo of the second example.
~ Click here for the source files of this demo.
No comments:
Post a Comment