You are currently viewing How to Use SimpleXML Extension in PHP | Pros and Cons

How to Use SimpleXML Extension in PHP | Pros and Cons

Simplexml Extension in PHP

PHP supports both DOM and SAX parsing methods. The simple and easiest way to work with XML data in PHP is via its SimpleXML extension. This extension, which is enabled by default in PHP 5, provides a user-friendly and intuitive interface for reading and processing XML data in a PHP application.

Advantages

  • We can easily get the name of the XML elements and text.
  • Compared to DOM or other parsers, SimpleXML has just a few lines of code to read text data from XML elements.
  • Basic tasks like reading/extracting data from XML files or strings, and editing text nodes or attributes are very fast.

Disadvantages

  • Not suitable for advanced XML documents which contain namespaces etc.
    In order to use SimpleXML, it is needed to create a SimpleXMLElement object variable containing XML data. After XML data has been loaded, any value in the object tree can be accessed the same way arrays or other variable data in PHP is accessed.

SimpleXMLElement object can be created by using the below function:

  • Simplexml_load-file():  This function is to be used when XML data needs to be loaded and parsed from a text file. Pass the pathname of the file as an argument. The SimpleXMLElement object will be returned if there is no error in the XML input file.
  • Simplexml_load_string(): This function is to be used when a PHP string variable or constant is to be loaded and parsed into a SimpleXMLElement object. SimpleXMLElement object will be returned if there is no error in the string data.
  • Simplexml_load_dom(): This function is to be used to load a DOMdocument object containing an XML object tree.
READ ALSO  What are Primary key and Foreign key in MySQL

How to access data/elements in the XML object tree after it has been parsed into SimpleXMLElement object.

    • 2. To access element values, called nodes of the XML tree by name as if they were properties of the SimpleXMLElement object. The value of the element will be returned as an element object. The below example displays the name of the college available in ‘college.xml’.
      echo $xml->name;
    • In cases where more than one element of a given name exists at the same level, including in square brackets the number of elements that need to access. Below statement access the HOD of the first department in the tree.
      echo "HOD of First Department: ".xml->department[0]->hod;
    • To access the element attribute, call nodes of the XML tree by the name as necessary, taking care to include the name of the attribute in brackets.
      echo "Faculty name of the first Department: ".xml->department[0] ['faculty'];

Note: foreach loop can be used to iterate through when multiple tags are available in the same name. For example, the “college.xml” contains three department tags and each department tag consists of three staff tags in nesting.

Leave a Reply