xml

XML stands for eXtensible Markup Language.

XML is designed to transport and store data.

XML is important to know, and very easy to learn.

PHP SimpleXML Examples

note.xml:

<?xml version=”1.0″ encoding=”UTF-8″?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don’t forget me this weekend!</body>
</note>

note.php

<?php
$xml=simplexml_load_file(“note.xml”);
print_r($xml);
?>

output: SimpleXMLElement Object ( [to] => Tove [from] => Jani [heading] => Reminder [body] => Don’t forget me this weekend! )

_______________________________________________________________________________________________

Output the element’s name and data for each child node:

<?php
$xml=simplexml_load_file(“note.xml”);
echo $xml->getName() . “<br>”;

foreach($xml->children() as $child)
{
echo $child->getName() . “: ” . $child . “<br>”;
}
?>

output:   note
to: Tove
from: Jani
heading: Reminder
body: Don’t forget me this weekend!

_______________________________________________________________________________________________

data write to XML file using php file

<?php

$xml = new DOMDocument();
$xml_album = $xml->createElement(“Album”);
$xml_track = $xml->createElement(“Track”);
$xml_album->appendChild( $xml_track );
$xml->appendChild( $xml_album );

$xml->save(“upload/test.xml”);

?>