VOOZH about

URL: https://www.geeksforgeeks.org/php/how-to-load-xml-data-into-mysql-using-php/

⇱ How to Load XML Data into MySQL using PHP ? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Load XML Data into MySQL using PHP ?

Last Updated : 23 Jul, 2025

In this article, we are going to store data present in XML file into MySQL database using PHP in XAMPP server.

XML: Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. The design goals of XML focus on simplicity, generality, and usability across the Internet.

Example:

Loading XML file: We will use simplexml_load_file() function to convert the well-formed XML document into the given file to an object.


 

Syntax:


 

SimpleXMLElement simplexml_load_file( string $filename, 
 string $class_name = "SimpleXMLElement",
 int $options = 0, string $ns = "", 
 bool $is_prefix = FALSE )


 

Steps to Write and Execute code:


 

  • Start XAMPP
👁 Image
  • Identify number of attributes in xml file and create table in XAMPP. There are 4 attributes in XML file (input.xml is the file name). These are title, link. description, keywords. The database name is xmldata and table name is xml
👁 Image


 

Filename: input.xml


 

Filename: index.php


 

Execution steps:


 

1. Save 2 files in one folder in path: xampp/htdocs/gfg


 

👁 Image


 

2. Type localhost/gfg/index.php and see the output


 

👁 Image
output


 

Now check the data in xml stored in our database or not


 

👁 Image


 

Comment
Article Tags: