← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 21 of 24

PHP SimpleXML Parser

XML data -- RSS feeds, configuration files, और कई legacy APIs के लिए इस्तेमाल होता है -- का एक nested, tag-based structure है जो JSON के simpler key-value shape से काफी अलग है। SimpleXML XML पढ़ने का PHP का सबसे आसान तरीका है: यह एक XML document को सीधे एक PHP object में convert कर देता है जिसकी properties XML की अपनी tag structure को mirror करती हैं।
Syntax
php
$xml = simplexml_load_string($xml_string);   // from text
$xml = simplexml_load_file("file.xml");       // from a file
echo $xml->child->grandchild;

एक XML String Parse करना

simplexml_load_string($xmlText) XML text की एक string parse करता है और document के structure को represent करने वाला एक SimpleXMLElement object return करता है -- हर XML tag एक object property बन जाती है जिसे आप normal -> operator से access कर सकते हैं, किसी भी दूसरे PHP object की तरह।

उदाहरण: Parsing an XML String

php
<?php
// Declare `$xml`, set to `simplexml_load_string("<catalog><book>PHP Basics</book></catalog>")`
$xml = simplexml_load_string("<catalog><book>PHP Basics</book></catalog>");
// Print `get_class($xml)` to the output
echo get_class($xml);
?>

एक File से XML Load करना

simplexml_load_file($path) exactly simplexml_load_string() जैसा काम करता है, लेकिन XML को सीधे एक file path या एक URL से पढ़ता और parse करता है, आपको पहले file के contents को manually एक string में पढ़ने से बचाते हुए।

उदाहरण: Loading XML from a File

php
<?php
// Call `file_put_contents("data.xml", "<catalog><book>PHP Basics</book></catalog>")`
file_put_contents("data.xml", "<catalog><book>PHP Basics</book></catalog>");
// Declare `$xml`, set to `simplexml_load_file("data.xml")`
$xml = simplexml_load_file("data.xml");
// Print `$xml->book` to the output
echo $xml->book;
?>

Nested Elements Access करना

Nested XML tags सीधे nested object properties में translate होती हैं -- <catalog><book><title>...</title></book></catalog> $catalog->book->title बन जाता है, आपको किसी भी nested PHP object के लिए इस्तेमाल किए जाने वाले same -> chaining से XML के structure में नीचे चलने देते हुए।

उदाहरण: Accessing Nested Elements

php
<?php
// Declare `$xml`, set to `simplexml_load_string("<catalog><book><title>PHP Basics</title></book></catalog>")`
$xml = simplexml_load_string("<catalog><book><title>PHP Basics</title></book></catalog>");
// Print `$xml->book->title` to the output
echo $xml->book->title;
?>

Repeated Elements पर Loop चलाना

जब किसी XML document में same tag name वाले कई sibling elements हों (जैसे एक <catalog> के अंदर कई <item> entries), SimpleXML उन्हें एक iterable collection की तरह treat करता है -- foreach से $catalog->item पर loop चलाना हर एक को बारी-बारी visit करता है।

उदाहरण: Looping Over Repeated Elements

php
<?php
// Declare `$xml`, set to `simplexml_load_string("<catalog><item>A</item><item>B</item></catalog>")`
$xml = simplexml_load_string("<catalog><item>A</item><item>B</item></catalog>");
// Loop over `$xml->item`, binding each item to `$item`
foreach ($xml->item as $item) {
    // Print `$item . "\n"` to the output
    echo $item . "\n";
}
?>

SimpleXML को एक Array में Convert करना

जबकि SimpleXMLElement objects direct property access के लिए अच्छे से काम करते हैं, एक को json_decode(json_encode($xml), true) से एक plain PHP array में convert करना एक handy trick है जब आप object syntax के बजाय ordinary array functions से data के साथ काम करना चाहें।

उदाहरण: Converting SimpleXML to an Array

php
<?php
// Declare `$xml`, set to `simplexml_load_string("<catalog><book>PHP Basics</book></catalog>")`
$xml = simplexml_load_string("<catalog><book>PHP Basics</book></catalog>");
// Declare `$array`, set to `json_decode(json_encode($xml), true)`
$array = json_decode(json_encode($xml), true);
// Print a human-readable dump of `$array`
print_r($array);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि SimpleXML objects हर operation के लिए exactly plain arrays या objects जैसे behave करते हैं -- कुछ चीज़ें (जैसे repeated elements पर isset checks) उम्मीद से थोड़ी अलग काम करती हैं।
  2. एक false result से properties पढ़ने की कोशिश करने से पहले यह check करना भूल जाना कि simplexml_load_file() या simplexml_load_string() actually succeed हुआ।
  3. string comparisons या concatenation में इस्तेमाल करने से पहले एक SimpleXMLElement value को string या उपयुक्त type में cast न करना, क्योंकि यह technically अपना खुद का object type है।
चैप्टर सारांश
  • simplexml_load_string($xmlText) एक XML string को एक SimpleXMLElement object में parse करता है।
  • simplexml_load_file($path) वही करता है, सीधे एक file path या URL से load करते हुए।
  • Nested XML tags nested object properties बन जाती हैं, normal -> operator से accessed।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.