In this post, you'll learn how to parse XML into an array in PHP. SimpleXML is a PHP extension that makes this possible. Show
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 <?xml version='1.0'?>
53 function.As you can see, you should see the SimpleXML section in the output of the <?xml version='1.0'?>
53 function.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 <?xml version='1.0'?>
55 function. The <?xml version='1.0'?>
55 function allows you to read and parse the XML file in a single call. On the other hand, if you have an XML string which you want to convert into an XML document object, you can use the <?xml version='1.0'?>
57 function.You could also use the <?xml version='1.0'?>
58 function to read the file contents and pass the resulting string to the <?xml version='1.0'?>
57 function, which eventually parses it into an object. Alternatively, if you prefer the object-oriented way, you could also use the <?xml version='1.0'?>
60 class and its utility methods to convert an XML string into an object.In this article, we’re going to use the <?xml version='1.0'?>
55 function to read an XML file, and we’ll see how to convert it into an array. Next, we’ll go through a real-world example to demonstrate how to do this.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. 1 <?xml version='1.0'?>
2 <employees>
3 <employee id="1"> 4 <name>John</name> 5 <email>[email protected]</email> <?xml version='1.0'?>
0<?xml version='1.0'?>
1<?xml version='1.0'?>
2<?xml version='1.0'?>
3<?xml version='1.0'?>
4<?xml version='1.0'?>
5<?xml version='1.0'?>
6<?xml version='1.0'?>
7<?xml version='1.0'?>
8<?xml version='1.0'?>
920 21 22 23 24 25 26 27 28 <?xml version='1.0'?>
1<employees>
0<employees>
1<employees>
2<employees>
3<employees>
4<employees>
5<employees>
6<?xml version='1.0'?>
9<employees>
821 30 31 As you can see, the employees.xml file contains the names and emails of employees. It’s important to note that the <?xml version='1.0'?>
67 value is passed as an attribute of the <?xml version='1.0'?>
68 tag.Next, create the simplexml.php file with the following contents. 1 33 2 35 3 4 38 5 <?xml version='1.0'?>
0<employee id="1">1 <?xml version='1.0'?>
2<employee id="1">3 <?xml version='1.0'?>
4<employee id="1">5 <?xml version='1.0'?>
6<employee id="1">7 <?xml version='1.0'?>
8<employee id="1">9 20 41 22 43 24 26 46 28 48 <employees>
0<employees>
2<name>John</name>1 <employees>
4<name>John</name>3 <employees>
6<name>John</name>5 Let’s go through the important snippets in the above example to understand how it works. Read and Parse the XML FileFirstly, we’ve used the <?xml version='1.0'?>
69 function to disable standard libxml errors and enable user error handling. In this way, we can catch the XML errors during parsing and display them in a user-friendly way. This is also really useful in the development phase.Next, we’ve used the <?xml version='1.0'?>
55 function to read and parse the employees.xml file. The first argument of the <?xml version='1.0'?>
55 function is a path to the XML file. It’s important to note that you need to adjust this path as needed. In the above example, it’s assumed that the employees.xml file is at the same directory level as that of the simplexml.php file. If the XML file is parsed successfully, it returns an object of the <?xml version='1.0'?>
60 class; otherwise, it returns <?xml version='1.0'?>
73.Next, if the <?xml version='1.0'?>
74 variable is set to <?xml version='1.0'?>
73, there was a problem parsing the employees.xml file. In that case, we use the <?xml version='1.0'?>
76 function to get all the errors and display them for debugging purposes. The <?xml version='1.0'?>
74 variable is set to <?xml version='1.0'?>
78 if the employees.xml file is parsed successfully.Convert the <?xml version='1.0'?> 60 Object Into Its JSON RepresentationNext, we’ve used the <?xml version='1.0'?>
65 function to convert the <?xml version='1.0'?>
60 object into its JSON representation. We’ve stored the JSON string in the <?xml version='1.0'?>
82 variable, as shown in the following snippet.1 46 Decode the JSON String Into an ArrayFinally, we’ve used the <?xml version='1.0'?>
66 function to decode the JSON string data. It’s important to note that we’ve passed <?xml version='1.0'?>
78 in the second argument of the <?xml version='1.0'?>
66 function, which converts all objects into associative arrays.1 48 Had you not passed <?xml version='1.0'?>
78 in the second argument, the <?xml version='1.0'?>
87 function would have produced objects of type <?xml version='1.0'?>
88 instead of arrays.So that's an outline of the whole process. Let’s run the simplexml.php file, and you should see the following output. 1 51 2 53 3 55 4 57 5 59 <?xml version='1.0'?>
0<email>[email protected]</email>1 <?xml version='1.0'?>
2<email>[email protected]</email>3 <?xml version='1.0'?>
4<email>[email protected]</email>5 <?xml version='1.0'?>
6<email>[email protected]</email>7 <?xml version='1.0'?>
8<email>[email protected]</email>9 20 <?xml version='1.0'?>
0122 <?xml version='1.0'?>
0324 <?xml version='1.0'?>
0526 <email>[email protected]</email>5 28 <?xml version='1.0'?>
09<employees>
0<?xml version='1.0'?>
11<employees>
2<?xml version='1.0'?>
13<employees>
4<email>[email protected]</email>9 <employees>
6<?xml version='1.0'?>
17<employees>
830 <?xml version='1.0'?>
20<?xml version='1.0'?>
21<email>[email protected]</email>1 <?xml version='1.0'?>
23<email>[email protected]</email>3 <?xml version='1.0'?>
25<email>[email protected]</email>5 <?xml version='1.0'?>
27<?xml version='1.0'?>
28<?xml version='1.0'?>
29<email>[email protected]</email>9 <?xml version='1.0'?>
31<?xml version='1.0'?>
32<?xml version='1.0'?>
33<?xml version='1.0'?>
34<?xml version='1.0'?>
35<?xml version='1.0'?>
05<?xml version='1.0'?>
37<email>[email protected]</email>5 <?xml version='1.0'?>
39<?xml version='1.0'?>
40<?xml version='1.0'?>
41<?xml version='1.0'?>
42<?xml version='1.0'?>
43<?xml version='1.0'?>
44<?xml version='1.0'?>
45<email>[email protected]</email>9 <?xml version='1.0'?>
47<?xml version='1.0'?>
17<?xml version='1.0'?>
49<?xml version='1.0'?>
50<?xml version='1.0'?>
51<?xml version='1.0'?>
52As expected, the array contains all the documents. As you may have noticed, it has also parsed the <?xml version='1.0'?>
89 field, which is attached with each document in the form of an attribute, and you can access it with the <?xml version='1.0'?>
90 key, as shown in the above output. Basically, everything is available in the form of key-value pairs. So you have a complete array at your disposal for further processing.So that’s how you can convert XML content into an array in PHP. It was a very simple example, but it should give you some insight into the whole process. ConclusionIn this article, we discussed how to convert an XML file into an array using PHP and SimpleXML. The SimpleXML extension is really useful when it comes to parsing and manipulating XML documents. The Best PHP Scripts on CodeCanyonThe free libraries on Packagist are wonderful for basic functionality—the foundation for a good app. However, for more specialized features or for complete applications that you can use and customize, take a look at the professional PHP scripts on CodeCanyon. Explore thousands of the best and most useful PHP scripts ever created on CodeCanyon. Here are a few of the best-selling and up-and-coming PHP scripts available on CodeCanyon for 2020.
Learn PHP With a Free Online CourseIf you want to learn PHP, check out our free online course on PHP fundamentals! In this course, you'll learn the fundamentals of PHP programming. You'll start with the basics, learning how PHP works and writing simple PHP loops and functions. Then you'll build up to coding classes for simple object-oriented programming (OOP). Along the way, you'll learn all the most important skills for writing apps for the web: you'll get a chance to practice responding to GET and POST requests, parsing JSON, authenticating users, and using a MySQL database. What is the XML in PHP?PHP XML Parser Introduction
XML is a data format for standardized structured document exchange. More information on XML can be found in our XML Tutorial. This extension uses the Expat XML parser. Expat is an event-based parser, it views an XML document as a series of events.
How to get XML file in PHP?Use simplexml_load_file function to load external XML file in your PHP program and create an object. After that, you can access any element from the XML by this object as follows. $xmldata = simplexml_load_file("employees.
What are XML libraries?As the name implies, XML is a library for verifying contents of XML files. In practice, it is a pretty thin wrapper on top of Python's ElementTree XML API.
What are the XML parsers available in PHP?In PHP there are two major types of XML parsers:. Tree-Based Parsers.. Event-Based Parsers.. |