close[x]


PHP

PHP-Home PHP-Environment Setup PHP-Syntax PHP-Run PHP in XAMPP PHP-Variable PHP-Comment PHP-Datatype PHP-String PHP-Operators PHP-Decision PHP-loop PHP-Get/Post PHP-Do While loop PHP-While loop PHP-For loop PHP-Foreach loop PHP-Array PHP-Multidimensional Arrays PHP-Associative Arrays PHP-Indexed Arrays PHP-Function PHP-Cookies. PHP-Session PHP-File upload PHP-Email PHP-Data & Time PHP-Include & Require PHP-Error PHP-File I/O PHP-Read File PHP-Write File PHP-Append & Delete File PHP-Filter PHP-Form Validation PHP-MySQl PHP-XML PHP-AJAX



learncodehere.com




PHP - XML

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.


HTML list that is valid XML

<ui>
<li>code now</li>
<li>code always</li>
<li>code tomorrow</li>
</ui>

Every opened tag in an XML document must be closed.


Parsing an XML Document

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.


Example : Parse an XML


<?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 −

Result :

SimpleXMLElement Object ( [to] => Mr. Mark john [from] => Lara Mosh [heading] => Assignment Submission [body] => PHP XML Tutorials )



PHP SimpleXML - Get Node/Attribute Values

SimpleXML is a PHP extension that allows us to easily manipulate and get XML data.

Lets See this get values from XML by example.

Example : Create XML

Save File : books.xml


 <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>
                

Example :Get XML

Save File : getxml.php



<?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 −

Result


    PHP,Sara Mohammed,2005,30.00
    HTML,Jemal Aron,2005,29.99