Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

32. XML Integration in PHP

Lesson 29 of 29 in the free PHP Programming notes on Siksha Sarovar, written by Rohit Jangra.

XML Integration in PHP

What is XML?

XML (eXtensible Markup Language) is a markup language used to store and transport data in a structured and readable format.

Exam Definition: XML is a self-descriptive language used for data storage and exchange.

Features of XML

  • Platform independent
  • Human and machine readable
  • Supports hierarchical data
  • Used in web services

XML vs HTML (Exam Comparison)

XMLHTML
Stores dataDisplays data
User-defined tagsPredefined tags
Case-sensitiveNot case-sensitive

---

Creating an XML File from PHP with Database Records

Steps

  1. Connect to database
  2. Fetch records
  3. Create XML structure
  4. Save XML file

Example: Create XML from Database

$conn = mysqli_connect("localhost","root","","testdb");
$result = mysqli_query($conn,"SELECT * FROM users");

$xml = new SimpleXMLElement("<users/>");

while($row = mysqli_fetch_assoc($result)){
   $user = $xml->addChild("user");
   $user->addChild("id",$row['id']);
   $user->addChild("name",$row['name']);
   $user->addChild("email",$row['email']);
}

$xml->asXML("users.xml");
Exam Line: SimpleXMLElement is used to create XML in PHP.

---

Reading Information from XML File

Using simplexml_load_file()

$xml = simplexml_load_file("users.xml");

foreach($xml->user as $u){
   echo $u->name."<br>";
}

Using simplexml_load_string()

$data = file_get_contents("users.xml");
$xml = simplexml_load_string($data);

---

Advantages of XML Integration

  • Easy data sharing
  • Platform independent
  • Structured format
  • Used in APIs and web services

Web Services vs XML (Exam View)

AspectWeb ServiceXML
PurposeCommunicationData format
UsageAPI interactionData storage
DependencyUses XML/JSONIndependent