Does PHP & XML work together in the same place ? LoL , Yes, they do !

PHP and XML







XML stands for Extensible Markup Language. A markup language is used to annotate text or add additional information. These annotations are not shown to the end-user, but are needed by the ‘machine’ to read and subsequently process the text correctly.An XML document is plain text and contains tags delimited by < and >.

XML plays a significant role in the present world of web development, it is perfectly useful for those who wish to make use of web technologies for distributing information across the web.

XML is used to format a document with a web browser. It is an influential and effectual tool to process a document’s contents and therefore, creating own tags is possible with XML. It works well with any operating system and maintains a great amount of flexibility, which is very essential for the web development scenario. 

Parsing an XML Document

Parsing is usually applies to text  it is  the act of reading text and converting it into a more useful in-memory format, "understanding" what it means to some extent.
An XML Parser is a parser that is designed to read XML and create a way for programs to use XML.

PHP - Simple XML

PHP 5's new SimpleXML module makes parsing an XML document, well, simple. It turns an XML document into an object that provides structured access to the XML.

The simple XML parser is used to parse Name, attributes and textual content.

The simple XML functions are shown below:


simplexml_load_file():

This function accepts file path as a first parameter and it is mandatory:
simplexml_load_file(($fileName,$class,$options,$ns,$is_prefix)

simplexml_load_string():

This function accepts the string instead of file reference.
simplexml_load_string($XMLData,$class,$options,$ns,$is_prefix)

simplexml_import_dom():

This function accepts DOM formatted XML content and it converts into simple XML.
simplexml_load_string($DOMNode,$class)

Assuming we’ve got our XML file above saved as a file called sctpl.xml in the same folder as our php file, we can read the whole feed into an object with the following code.

<?php
    $sctpl = consultancy('sctpl.xml');
?>

PHP - Simple XML GET:

Get Node Values:

Consider following XML file:
sctpl.xml:
<SCTPL>

<Location >
<address>4/B wing,Trishul Apartments,Sindhi Society Rd Number1,Chembur, Mumbai.
</address>
<phno> 022-25277413 </phno>
<facebook>https://www.facebook.com/suvenconsultants/</facebook>
<youtube>https://www.youtube.com/user/rockyjagtiani</youtube>
<Location>

</SCTPL>


Following Code will return value of address,phno,facebook,youtube nodes:

Example

<?php
$xml=simplexml_load_file("sctpl.xml") or die("Error: Cannot create object");
echo $xml->Address . "<br>";
echo $xml->phno . "<br>";
echo $xml->facebook . "<br>";
echo $xml->youtube; ?>

Output of above code will be:
4/B wing,Trishul Apartments,Sindhi Society Rd Number 1,Chembur, Mumbai 
022-25277413

https://www.facebook.com/suvenconsultants/
https://www.youtube.com/user/rockyjagtiani


Get Node Values of Specific Elements

Consider another XML file:

SuvenConsultancy.xml:
<SCTPL>

<Location name="Chembur">
<address>4/B wing,Trishul Apartments,Sindhi Society Rd Number1,Chembur, Mumbai.
</address>
<phno> 022-25277413 </phno>
<facebook>https://www.facebook.com/suvenconsultants/</facebook>
<youtube>https://www.youtube.com/user/rockyjagtiani</youtube>
<Location>

<Location name="Kandivali">
<address>510, 5th Floor , Ghanshyam Enclave,Link Road, Lalji-pada JunctionKandivali West, Mumbai
</address>
<phno> 022-28674177</phno>
<facebook>https://www.facebook.com/suvenconsultants/</facebook>
<youtube>https://www.youtube.com/user/rockyjagtiani</youtube>
<Location>

</SCTPL>

The following example gets the node value of the <phno> element in the first and second <location>elements in the "SuvenConsultancy.xml" file: 

Example

<?php
$xml=simplexml_load_file("SuvenConsultancy.XML"or die("Error: Cannot create object");
echo $xml->location[0]->phno"<br>";
echo $xml->location[1]->phno;
?>
The output of the code above will be:
022-25277413
022-28674177

Get Attribute Values:

The following example gets the attribute value of the "name" attribute of the first <location> element:

Example

<?php
$xml=simplexml_load_file("SuvenConsultancy.xml"or die("Error: Cannot create object");
echo $xml->location[0]['name'];
?>
The output of the code above will be:
Chembur

Want to learn Web Technologies?

Popular posts from this blog

MongoDB - daTa ModeLLinG Concepts: SQL - Where all to UsE ?

Comparing Bootstrap With Google's Material Design Lite : Bootstrap - ResponSive Design

MongoDB overView with Installation_procEss: SQL - Where all to UsE ?