Financial Calculator

Calculate loans, SIP, PPF, NPS & more!

Financial Calculator

Calculate loans, SIP, PPF, NPS & more!

PHP SimpleXML Parsers

PHP

PHP SimpleXML Parser

SimpleXML PHP का सबसे आसान और beginner-friendly XML parser है। यह एक XML document को PHP object के रूप में बदल देता है, जिससे XML elements और attributes को access करना बहुत आसान हो जाता है।

  • SimpleXML XML data को object में convert करता है।
  • XML पढ़ना, elements तक पहुँचना और data निकालना आसान बनाता है।
  • SimpleXML छोटे और मध्यम आकार की XML files के लिए सबसे बेहतर विकल्प है।

SimpleXML Functions

  • simplexml_load_string() – XML string को load करता है।
  • simplexml_load_file() – XML file को load करता है।

SimpleXML: Load XML String

हम XML को एक simple string के रूप में load कर सकते हैं और फिर उसे loop कर सकते हैं।

उदाहरण: simplexml_load_string()
<?php
$xml = simplexml_load_string(
    " <books> <book>PHP Tutorial</book> <book>JavaScript Basics</book> </books> "
);

foreach ($xml->book as $item) {
    echo $item . "<br>";
}

?>
Output
PHP Tutorial
JavaScript Basics

SimpleXML: Load XML File

अगर XML data किसी external file में है, तो हम simplexml_load_file() function का उपयोग कर सकते हैं।

उदाहरण: simplexml_load_file()
<?php
$xml = simplexml_load_file("data.xml");
foreach ($xml->item as $i) {
    echo $i . "<br>";
}

?>
Output
Output your XML file content

Accessing Attributes

SimpleXML XML elements के attributes को access करना बहुत आसान बना देता है।

उदाहरण: Accessing XML Attributes
<?php
$xml = simplexml_load_string(
    " <users> <user id='101'>Rahul</user> <user id='102'>Amit</user> </users> "
);

foreach ($xml->user as $u) {
    echo "ID: " . $u['id'] . " - " . $u . "<br>";
}

?>
Output
ID: 101 - Rahul
ID: 102 - Amit

Child Elements & Nesting

XML में nested elements को भी SimpleXML आसानी से handle कर लेता है।

उदाहरण: Nested XML Elements
<?php
$xml = simplexml_load_string(
    " <library> <book> <title>PHP Guide</title> <author>John</author> </book> </library> "
);

echo "Title: " . $xml->book->title . "<br>";
echo "Author: " . $xml->book->author;

?>
Output
Title: PHP Guide
Author: John
Article By: Brajlal Prasad
Created on: 03 Nov 2025  12  Views
 Print Article
Report Error

If you want to report an error, or any suggestion please send us an email to [email protected]

Financial Calculator

Financial Calculator

Take control of your finances with our powerful Financial Calculator app—calculate loans, SIP, PPF, NPS and more all in one place!

Play Store