XML is one of the most famous formats that are used to share data across the web. An XML file is similar to HTML file. However XML is far more flexible when compared with HTML. Naming conventions of XML tags are extremely flexible. In HTML you can have only specific tags with predefined names such as “div” for defining a div and “table” for creating a table. In XML, you can have tags with any names e.g.
Mike 10 Male City High School "; $outputxml=simplexml_load_string($StudentInfo) or die("Xml file cannot be read"); print_r($outputxml); ?>
Download the code
Run the code
In the above code, student information has been stored in the form of XML data. You can see that XML tags used here are purely random. As long as you define your XML tags using proper opening and closing symbol, you can name your tag anything. To read XML data that is in the string form SimpleXML parser called “simplexml_load_string” is used. This function takes XML string as input and converts it into associative array where tag name is converted to index and value inside the tag is converted to corresponding index value. In the output you will that associative array.
Reading XML data from a file
To read an XML file, another parser function “simplexml_load_file” is used. This function takes path of the file to be read as a parameter. To see how this works. create a file named “student.xml” and copy the following data in it.
Now, the following PHP script will load and read the contents of this file.
Download the code
Run the code
The output of the above code will be similar to that of the first example.