Thursday, October 15, 2020

PHP Simple XML Parser

Hello folks! welcome to another section of my tutorial on PHP. In this section of my PHP tutorial, we will be discussing about the PHP Simple XML Parser. 

The simple xml parser is used to parse Names, attributes, and textual contents.

Simple XML Functions

Simple xml functions are shown below -


This function accepts file path as a first parameter and its very compulsory.



This function accepts the string instead of file reference.



This xml function accepts Dom formatted xml content and then converts into simple xml.



The below example shows how to parse an XML data file -

   $data = "<?xml version = '1.0' encoding = 'UTF-8'?>
      <Course>Web Development</Course>
      <Company>Web Design Tutorialz</Company>
   $xml = simplexml_load_string($data) or die("Error: Cannot create object");



When the above code is executed, it will produce the following result -

SimpleXMLElement Object ( [Course] => Web Development [Subject] => HTML [Company] => Web Design Tutorialz [Price] => $20 )

We can also call an xml data file as shown below and it produces the same result as shown above -

   $xml = simplexml_load_file("data") or die("Error: Cannot create object");

Alright guys! This is where we are rounding up for this tutorial post. In my next tutorial guide, we are going to be discussing about PHP - Simple XML GET.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.


Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.