PHP Tutorial
XML is a markup language that looks a lot like HTML.
XML is extremely picky about document structure.
XML is very strict when it comes to document structure.
Every opened tag in an XML document must be closed.
It turns an XML document into an object that provides structured access to the XML.
To create a SimpleXML object from an XML document stored in a string, pass the string to simplexml_load_string( ). It returns a SimpleXML object.
<?php
$myXMLData =
"<?xml version='1.0' encoding='UTF-8'?>
<note>
<to>Mr. Mark john</to>
<from>Lara Mosh</from>
<heading>Assignment Submission</heading>
<body>PHP XML Tutorials </body>
</note>
XML;
$xml=simplexml_load_string($note);
print_r($xml);
?>
This will produce the following result −
SimpleXML is a PHP extension that allows us to easily manipulate and get XML data.
Lets See this get values from XML by example.
<bookstore>
<book category="programing">
<title lang="en">PHP</title>
<author>Sara Mohammed</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="web">
<title lang="en">HTML</title>
<author>Jemal Aron</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
<?php
$xml=simplexml_load_file("books.xml") or die("Error: Cannot create object");
foreach($xml->web() as $books) {
echo $books->title . ", ";
echo $books->author . ", ";
echo $books->year . ", ";
echo $books->price . "<br>";
}
?>
This will produce the following result −
PHP,Sara Mohammed,2005,30.00
HTML,Jemal Aron,2005,29.99