In article Introduction to xml in php you had notice i had already return the structure for my example for xml file example.xml file Screenshot1. In this article we are going to reading in different element of this data So, You can see in Screenshot1 we have producers tag is root tag which is important its must exits in every xml file its called root tag of xml file you create. Producers tag is the tag name you can create you own name tag. Our producers are root tag which are open and close at the end of the xml file. <producers> </producers> and inside here we get tag here called producer and we are ending it by </producer>. Inside producers tags we had 3 producer which contains we had elements name, and age for each producer tag. So, What we can do inside php once we open this xml file successfully we can say grape elements of first producer name or vice versa we can grape elements of second and third producer. Now inside our index.php file we are going to loading this xml file inside php file and access as in array we are loading this in as an array or rather each element in each producer to it. We can access in (->) Symbol here to each elements like producer , name etc. We can had many parents and children in xml file. we can access them by array.
Example of XML FILE exmaple.xml Screenshot1 |
Code for XML File (Screenshot1)
<producers> <producer> <name>alex</name> <age>21</age> </producer> <producer> <name>Billy</name> <age>16</age> </producer> <producer> <name>Dale</name> <age>49</age> </producer> </producers>
Code For Reading xml file in php (Screenshot2)
Reading XML File In PHP Screenshot2 |
<?php $xml = simplexml_load_file('example.xml'); echo $xml->producer(1)->name.' is '.$xml->producer(1)->age; ?>
Output Screenshot3
Screenshot3 Output |
Now Lets See example firstly we are opening our xml file creating new variable called $xml and i am going to be using this function called simplexml_load_file. Now simplexml_load_file is a new feature introduce in php5 that allows open a xml file using simplexml_load_file function and really easily access each element of xml file. Inside simplexml_load_file function we will specify example.xml So, now we successfully open up our xml file. Now we will returning some values. We don't need to include producers tag anywhere because its just a root tag about document its not important. But we can do is each producer tags and elements inside. we will echo $xml-> now we going to say producer which is the name on xml file producer tag. In xml everything is case sensitive So if suppose producer is Producer you had to use $xml-> Producer but now we are going to use producer tag only. In square brackets because we are going to choose which producer we want to echo data for. So here we are doing with array so, producer[0] is alex , producer[1] is billy, and producer[2] is Dale. So, Just for example I am going to echo out 1. So, it will give me element billy then now we want to access tag we want to display. So, i want to display name , age of producer[1] which is billy. So, $xml -> producer[1]-> name will display the name of the billy and $xml -> producer[1] -> age will display age of the billy and we had concatenate with is so its looks good you can see the output in Screenshot3.
So, Now you had a idea why it is useful they can be add in structure format using programming in php. It can also do with other web development languages. So, its very very useful tool.
Code for Reading xml file in php Screenshot4
Reading xml File as array in php Screenshot4 |
<?php $xml = simplexml_load_file('example.xml'); foreach ($xml->producer as $producer) { echo $producer->name.' is '.$producer->age.'<br>'; } ?>
Output Screenshot5
Screenshot5 |
Example of Loop so we can get names of all producer for this we will use FOREACH STATEMENT IN PHP. In foreach statement we can say foreach ($xml->producer as $producer) We had fetch all producer into variable called $producer So, for each $xml producer we are using $xml to open file of xml file So for each one of this producer we had named as $producer then we are displaying all producer in to the browser.
If you want to users all tweets on twitter you can use in same way as here in xml i had used for graphing data from the xml file. Reading data from xml file. Using foreach statement you can grape all data from the xml file and access them individual. The purpose of showing this tutorials reading to xml file is to show the structure of xml file if we know the structure of the xml file we can grape data from that xml file. We grape every single data from the xml file.
Now If we had a Child elements inside of our xml documents. In previous example Screenshot1 however what happens if you want to add items further information in about this producer.
Code for XML File
example.xml File
<producers> <producer> <name>Alex</name> <age>21</age> <show> <showname>A show</showname> <showdate>12th December 2010</showdate> </show> <show> <showname>Another Show</showname> <showdate>4th July 2009</showndate> </show> </producer> <producer> <name>Billy</name> <age>16</age> <show> <showname>Better Show</showname> <showdate>25th July 2007</showname> </show> </producer>
Code for Reading XML FIle In PHP
index.php
<?php $xml = simplexml_load_file('example.xml'); foreach ($xml->producer as $producer) { echo $producer->name.'('.$producer->age.')<br>; foreach ($producer->show as $show) { echo $show -> showname.' on '.$show->showdate; } } ?>
Output Screenshot7.
Screenshot7 |
Now inside each producer i want to specify a show that they both produce so for example in producer i had create child called show and i am going to end show and now going to provide some details about show tag inside show tag like showname tag and showdate tag. In 1st producer i had created 2 show tag with element inside it called showname and showdate. We also maintain the particular details about our producer. So, if we come to the database like structure we got name and age in producer then we got two different shows that in producer. So, we are not only accessing each producer we also access on the show tag which is inside the producer which is called child of producer tag. And also added one show to billy .In this way we had structure our xml file now let see how we can read this xml file from php script and display parents and child's into the browser.
Now Come to php page we already seen to add xml file in previous example and foreach statement in php. Looping through each producer as a variable producer now what we can do Echo out the producers name by using this structure we had established we can do the line break $producer->name.'<br >';It will give the name tag inside the each producer. Then $producer->name.'('.$producer->age.')<br>'; Now it will give name and age tags of each producer tag. So, we will get alex 21 and Billy 16. Now Echo $producer->show->showname.'<br>'; Will display the Show tag inside showname tag content Screenshot6. Now we will show in the proper manner. By foreach statement in php. foreach producer show tag will retrieve in variable $show foreach ($producer->show as $show) . Now We had taken each element of show we can access on each element of the show tag So, will echo out $show->showname.' on '. $show->showdate will we can had access on element of the show tag which is on our loop You can see the output on screenshot7.By same structure and tags will remain same our data is automatically updated from the xml file. So, this way we can use xml file for reading in php.
Screenshot6 |
No comments:
Post a Comment
Thanks For Comment Will get you Soon..