Table of Contents
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 –
- jQuery Copy Text to Clipboard Using Clipboard JS Plugin
- PHP & MySQLi How To Add Export Buttons To DataTable Tutorial
- Laravel 9 How To Get Current Location Details Tutorial
- jQuery Difference Between Two Dates in Hours Minutes and Seconds
Let’s get started.
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: firstname.lastname@example.org @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);
Open into browser.
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.
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.