XML TO PHP Tutorial: Meet SimpleXML

By Tejal Patwardhan


Introduction

XML is a language that is really good at transporting and storing data. It isn't made to display data. One great feature of XML is that you make up your own tags so that they are more applicable to what you want to do. SimpleXML is an easy way to use php to read XML files. This is going to be an interactive tutorial. By the end of it, you will have a basic understanding of SimpleXML. Hopefully you already know the basics of XML. If not, use this helpful tutorial to learn it. Then come back here. Let's get started!

Making a simple XML document to manipulate: a secret spy message

  • Start off by saving a document as "spy.xml" and with Western (ISO Latin 1) encoding.
  • Insert the following starting code into the document to render it as an xml document with Western encoding.
    <?xml version="1.0" encoding="ISO-8859-1"?> 
    Remove the spaces between the question marks and the carrots. Remember to insert the code at the very top of the document with no blank spaces. Blank spaces can interfere with the information we are trying to store.
  • We are making a secret note between two spies. Let us set the root element in this document as message.
  • <message>
    </message>
  • Next up, the content of the message. I have gone to the liberty of writing a secret message using 4 child elements: to, date, content, and signature. Add this code between your message root elements.
  • <to>Agent 1180463</to>
    <date>Jan 10</date>
    <content>Mission #GQ: DO NOT ENTER THE NEST</content>
    <signature>-X</signature>
  • Your XML document is now complete.
  • SimpleXML

  • Create a document named "SimpleXML.php" and save it in the same folder as the XML document.
  • Add the following to the body to the document
  • <?php
    $xml=simplexml_load_file("spy.xml");
    ?>
  • Let us explore several ways to display the XML document.
  • The first one of these is to simply print the message to the screen. Insert the following after the SimpleXML file load.
  • print_r($xml);
  • The contents of the XML will simply be printed to the screen. It should look like this:
  • The second one of these is to print the data in each element to the screen. Insert the following into the XML document.
  • echo $xml->to . "<br>";
  • 'echo' in php prints to the screen, and the $xml->to is accessing the data stored in the XML file in the element 'to.'
  • Do this multiple times for each of the elements
  • The contents of the XML will simply be printed to the screen. It should look like this:
  • The third one of these is to use some PHP functions.
  • getName() is a function that returns the name of the XML tag that is being referenced by the SimpleXML element.
  • children() finds the children of a specified node.
  • In this example, the variable $element is used to store the data from the specified tag.
  • Notice the mixture of PHP and XML language used here
  • Insert the following into the XML document.
  • foreach($xml->children() as $element)
      {
      echo $element->getName() . ": " . $element . "<br>" . "<br>";
      }
  • This loops through each of the root's children to display the element name and the data stored in the element.
  • The screen should look like this:
  • Conclusion

    Hopefully this tutorial was a useful place to start. I have taken the liberty of attaching a reference guide of functions you can use. I DO NOT OWN IT! It is from W3Schools. You can access the table online here, or you can look at the picture below.