1.simplexml_load_file
2.simplexml_load_string
Lets begin with simplexml_load_file.
simplexml_load_file takes a xml file as mandatory parameter and coverts it into an object.
On success returns the object of SimpleXmlElement or false on failure.
To try copy the xml below and save it as sample.xml
<message> <to>user1</to> <from>user2</from> <heading>testxml</heading> <body>I am testing php xml functions</body> </message>
<?php // check if file existx or not $file = 'sample.xml'; $file_exixts = (file_exists($file)); if($file_exixts) { $xml_contents = simplexml_load_file($file); } // check the file contents echo "<pre>"; print_r($xml_contents); echo "</pre>"; //accessing the value $to = $xml_contents->to; echo "This message was sent to $to"; ?>
simplexml_load_string takes a xml string as mandatory parameter and coverts it into an object.On success it also returns the object of SimpleXmlElement or false on failure.
<?php $xml_string = "<message> <to>user1</to> <from>user2</from> <heading>testxml</heading> <body>I am testing php xml functions</body> </message>"; $xml_object = simplexml_load_string($xml_string); // check the file contents echo "<pre>"; print_r($xml_object); echo "</pre>"; //accessing the value $to = $xml_object->to; echo "This message was sent to $to"; ?>
No comments:
Post a Comment