PHP SimpleXML Parser
In this page:
$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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
एक 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
// 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;
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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;
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि SimpleXML objects हर operation के लिए exactly plain arrays या objects जैसे behave करते हैं -- कुछ चीज़ें (जैसे repeated elements पर isset checks) उम्मीद से थोड़ी अलग काम करती हैं।
- एक false result से properties पढ़ने की कोशिश करने से पहले यह check करना भूल जाना कि simplexml_load_file() या simplexml_load_string() actually succeed हुआ।
- 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।
Chapter Quiz — Complete all 24 topics to unlock
0/24 topics done
Complete these topics first:
- PHP Date & Time
- PHP Math Functions
- PHP JSON Handling
- PHP XML Handling
- PHP cURL Introduction
- PHP REST API Basics
- PHP Composer & Packages
- PHP Autoloading
- PHP Design Patterns
- PHP MVC Architecture
- PHP Security Best Practices
- PHP Performance Optimization
- PHP 8 New Features
- PHP Type Declarations
- PHP Match Expression Advanced
- PHP Fibers
- PHP Attributes
- PHP Magic Constants
- PHP Include & Require
- PHP Iterables
- PHP SimpleXML Parser
- PHP SimpleXML Get
- PHP XML Expat Parser
- PHP DOM Parser