How To Read XML Data To Array in PHP Tutorial

Share this Article
Reading Time: 3 minutes
399 Views

Inside this article we will see the concept i.e How To Read XML Data To Array in PHP. Article contains classified information about reading XML data and convert into PHP array. You can use this concept to seed your MySQLi database table.

If you are looking for an article which gives the understanding about parsing a XML data into PHP Array then this article will help you to learn a lot from this tutorial.

The Extensible Markup Language (XML) is a simple text-based format for representing structured information: documents, data, configuration, books, transactions, invoices, and much more.

Learn More –

Let’s get started.

Application Programming

Suppose, you have a XML file named as books.xml. This XML file contains books data. We will parse this XML data to PHP Array.

Create a file called it as index.php inside your project folder where books.xml file kept.

Open index.php and write this code into it.

<?php
/*
 @Author: Sanjay Kumar
 @Project: How To Read XML Data To Array in PHP Tutorial
 @Email: onlinewebtutorhub@gmail.com
 @Website: https://onlinewebtutorblog.com/
*/

$xmlString = file_get_contents('books.xml');
$xmlObject = simplexml_load_string($xmlString);

$json = json_encode($xmlObject);
$phpArray = json_decode($json, true);

echo "<pre>";
print_r($phpArray);

Application Testing

Now,

Open into browser.

URL: http://localhost/php-xml-to-array/index.php

You can easily save or seed this PHP array into your database table.

We hope this article helped you to learn How To Read XML Data To Array in PHP Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5