XML Format in PHP
XML is a format for storing data. This format is often used for data exchange between sites, or between a server and a browser. Technically, XML is similar to HTML, but with any tags and attributes.
Let's create a separate file test.xml,
where we will store a test
XML document.
First, this document needs a special header that will indicate that we have XML and set the version of this language:
<?xml version="1.0"?>
Now we need to create a root element.
It will be a tag that
contains the entire document.
The name of this tag can be anything.
Let's call it <root>:
<?xml version="1.0"?>
<root>
</root>
Now let's add some data:
<?xml version="1.0"?>
<root>
<test>text</test>
</root>
Now in PHP we can load this
element using the function
simplexml_load_file:
<?php
$xml = simplexml_load_file('test.xml');
?>
A special object will be written to the variable, with which we can retrieve tag data from the XML tree:
<?php
var_dump($xml); // object
?>
Furthermore, in the following lessons, for brevity,
I will omit the moment of obtaining XML
and will assume that the variable
$xml stores the result of the function
simplexml_load_file.
Create a test XML file.
Retrieve it in PHP.
Output the retrieval result
via var_dump.