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”);

?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s