How to parse an xml document using php
SimpleXML is a PHP extension that allows us to easily manipulate and get XML data. Show
The SimpleXML ParserSimpleXML is a tree-based parser. SimpleXML provides an easy way of getting an element's name, attributes and textual content if you know the XML document's structure or layout. SimpleXML turns an XML document into a data structure you can iterate through like a collection of arrays and objects. Compared to DOM or the Expat parser, SimpleXML takes a fewer lines of code to read text data from an element. InstallationFrom PHP 5, the SimpleXML functions are part of the PHP core. No installation is required to use these functions. PHP SimpleXML - Read From StringThe PHP Assume we have a variable that contains XML data, like this: $myXMLData = The example below shows how to use the Example$myXMLData = $xml=simplexml_load_string($myXMLData) or die("Error: Cannot create object"); Run example » The output of the code above will be: SimpleXMLElement Object ( [to] => Tove [from] => Jani [heading] => Reminder [body] => Don't forget me this weekend! ) Error Handling Tip: Use the libxml functionality to retrieve all XML errors when loading the document and then iterate over the errors. The following example tries to load a broken XML string: Examplelibxml_use_internal_errors(true); $xml = simplexml_load_string($myXMLData); Run example » The output of the code above will be: Failed loading XML: PHP SimpleXML - Read From FileThe PHP Assume we have an XML file called "note.xml", that looks like this:
The example below shows how to use the Example $xml=simplexml_load_file("note.xml") or die("Error: Cannot create object"); Run example » The output of the code above will be: SimpleXMLElement Object ( [to] => Tove [from] => Jani [heading] => Reminder [body] => Don't forget me this weekend! ) Tip: The next chapter shows how to get/retrieve node values from an XML file with SimpleXML! More PHP SimpleXMLFor more information about the PHP SimpleXML functions, visit our PHP SimpleXML Reference. In this post, you'll learn how to parse XML into an array in PHP. SimpleXML is a PHP extension that makes this possible. In your day-to-day PHP development, sometimes you'll need to deal with XML content. Whether it’s exporting data as XML documents or processing incoming XML documents in your application, it’s always handy to have a
library that can perform these operations smoothly. When it comes to dealing with XML in PHP, there are different methods to choose from. In fact, there are different extensions available in PHP that allow you to read and parse XML documents. PHP’s XML parser is based on James Clark’s expat library, which is a stream-oriented XML library written in C. This XML parser library allows you to parse XML documents, but it’s not able to validate them. It’s event-based and stream-oriented, and
thus it’s really useful when you’re dealing with very large XML files—but a little more complicated than it needs to be for small files. The other option is the SimpleXML extension, which is one of the most popular extensions used by the PHP community to process XML documents. The SimpleXML extension allows you to parse XML documents very easily, just as if you were reading a file in PHP. In this article, we’re going to use the SimpleXML extension to demonstrate how you could convert XML content into an array. If you want to follow along with the example in this article, make sure that you’ve installed the SimpleXML extension in your PHP installation. The SimpleXML PHP ExtensionThe SimpleXML PHP extension provides a complete toolset which you can use to read, write and parse XML documents in your PHP applications. It’s important to note that the SimpleXML extension requires PHP 5 at a minimum. Also, it requires the libxml PHP extension. The
SimpleXML extension is enabled by default, but if you want to check if it's enabled in your PHP installation, you can check it quickly by using the As you can see, you should see the SimpleXML section in the output of the The SimpleXML extension provides different functions that you could use to read and parse XML content. Loading an XML String or File With SimpleXMLFor example,
if you want to parse the XML file, you can use the You could also use the In this article, we’re going to use the How to Convert XML to an Array With PHPIn this section, we’ll see how you can convert XML content into an array. First of all, let’s quickly go through the steps that you need to follow to convert XML content into an array with the help of the SimpleXML extension.
For demonstration purposes, let’s assume that we have an XML file as shown in the following snippet. We’ll call it employees.xml. It contains the basic details of all employees. Our aim is to convert it into an array that you could use for further processing.
As you can see, the employees.xml file contains the names and emails of employees.
It’s important to note that the Next, create the simplexml.php file with the following contents. message; } exit; } $objJsonDocument = json_encode($objXmlDocument); $arrOutput = json_decode($objJsonDocument, TRUE); echo ""; print_r($arrOutput); ?> |