Financial Calculator

Calculate loans, SIP, PPF, NPS & more!

Financial Calculator

Calculate loans, SIP, PPF, NPS & more!

PHP SimpleXML Get

PHP

PHP SimpleXML Get

SimpleXML XML data को PHP object में बदल देता है जिससे XML के elements, attributes और nested values को access (Get) करना बहुत आसान हो जाता है। PHP में XML पढ़ने के लिए SimpleXML सबसे सरल और beginner-friendly तरीका है।

  • XML elements को $xml->elementName से access किया जा सकता है।
  • XML attribute को $xml['attributeName'] से प्राप्त किया जाता है।
  • Nested data को object की तरह chain करके access किया जाता है।

simplexml_load_string() के साथ Data Get करना

नीचे एक simple XML string को load कर के उसके elements को access (Get) किया गया है।

उदाहरण: XML Elements Get using SimpleXML
<?php
$xml = simplexml_load_string(
  " <student> <name>Rahul</name> <course>PHP</course> </student> ");

echo "Name: " . $xml->name . "<br>";
echo "Course: " . $xml->course;

?>
Output
Name: Rahul
Course: PHP

Attributes Get करना (SimpleXML Attributes)

XML attributes को access करना भी काफी आसान है। बस attribute को array की तरह refer करते हैं:

उदाहरण: SimpleXML Get Attribute
<?php
$xml = simplexml_load_string(
  " <user id='101' city='Delhi'>Amit</user> ");

echo "ID: " . $xml['id'] . "<br>";
echo "City: " . $xml['city'];

?>
Output
ID: 101
City: Delhi

Nested Elements Get करना

SimpleXML nested XML hierarchy को object chaining की मदद से आसानी से access कर लेता है।

उदाहरण: Nested XML Elements Get
<?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

Loop के अंदर Elements Get करना

अगर XML में multiple same elements हों, तो उन्हें loop करके easily access किया जा सकता है।

उदाहरण: Loop में SimpleXML Get
<?php
$xml = simplexml_load_string(
  " <fruits> <fruit>Apple</fruit> <fruit>Mango</fruit> <fruit>Orange</fruit> </fruits> ");

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

?>
Output
Apple
Mango
Orange
Article By: Brajlal Prasad
Created on: 03 Nov 2025  7  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