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

PHP SimpleXML Get

Basic property access से आगे, SimpleXML dedicated methods का एक family offer करता है -- attributes(), children(), xpath() -- किसी XML document से specific values निकालने के ज़्यादा precise और powerful तरीकों के लिए, खासकर उस XML के लिए उपयोगी जिसका structure एक straightforward property chain handle कर सके उससे ज़्यादा complex या variable हो।
Syntax
php
foreach ($xml->attributes() as $name => $value) { }
foreach ($xml->children() as $child) { }
$results = $xml->xpath("//element_name");

attributes() से सभी Attributes पाना

एक XML element का attributes() method इसके सभी attributes को एक iterable collection की तरह return करता है, तब उपयोगी जब किसी element में कई attributes हों जिन्हें आप साथ process करना चाहें, हर एक को individually नाम से access करने के बजाय।

उदाहरण: Getting All Attributes with attributes()

php
<?php
// Declare `$xml`, set to `simplexml_load_string('<book id="1" lang="en">PHP Basics</book>')`
$xml = simplexml_load_string('<book id="1" lang="en">PHP Basics</book>');
// Loop over `$xml->attributes()`, binding each item to `$name => $value`
foreach ($xml->attributes() as $name => $value) {
    // Print "$name: $value\n" to the output
    echo "$name: $value\n";
}
?>

children() से Child Elements पाना

children() method किसी element के direct child elements को एक iterable collection की तरह return करता है, एक known property name सीधे access करने जैसा, लेकिन तब उपयोगी जब किसी document के exact child tag names पहले से न पता हों, या varying XML structures में generically काम करते समय।

उदाहरण: Getting Child Elements with children()

php
<?php
// Declare `$xml`, set to `simplexml_load_string("<book><title>PHP Basics</title><author>Alice</author></book>")`
$xml = simplexml_load_string("<book><title>PHP Basics</title><author>Alice</author></book>");
// Loop over `$xml->children()`, binding each item to `$child`
foreach ($xml->children() as $child) {
    // Print `$child->getName() . ": " . $child . "\n"` to the output
    echo $child->getName() . ": " . $child . "\n";
}
?>

XPath से Query करना

xpath($query) document के against एक XPath query चलाता है -- XML में navigate करने के लिए एक purpose-built pattern language -- हर matching element का एक array return करते हुए।

XPath एक लंबी property chain लिखे बिना deeply nested या conditionally-matched elements तक पहुँचने के लिए खासतौर पर powerful है।

उदाहरण: Querying with XPath

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>");
// Declare `$results`, set to `$xml->xpath("//title")`
$results = $xml->xpath("//title");
// Print `$results[0]` to the output
echo $results[0];
?>

Text Content बनाम Elements पाना

किसी SimpleXMLElement पर (string) call करना सिर्फ इसका plain text content extract करता है, इसके object-like structure को discard करते हुए -- तब उपयोगी जब आपको specifically raw text value चाहिए न कि एक SimpleXMLElement object, जैसे इसे एक string comparison या database query में इस्तेमाल करने से पहले।

उदाहरण: Getting Text Content vs Elements

php
<?php
// Declare `$xml`, set to `simplexml_load_string("<book>PHP Basics</book>")`
$xml = simplexml_load_string("<book>PHP Basics</book>");
// Declare `$text`, set to `(string) $xml`
$text = (string) $xml;
// Print a detailed dump (with types) of `$text`
var_dump($text);
?>

Namespaced XML Handle करना

कुछ XML documents namespaces इस्तेमाल करते हैं -- tag names पर atom: जैसा एक prefix एक document में मिलाई गई अलग-अलग XML vocabularies के बीच naming conflicts से बचने के लिए -- और namespaced elements को सही से access करने के लिए SimpleXML को children() और attributes() methods को explicitly namespace URI देना ज़रूरी है।

उदाहरण: Handling Namespaced XML

php
<?php
// Declare `$xml`, set to `simplexml_load_string('<feed xmlns:atom="http://www.w3.org/2005/Atom"><atom:title>My Feed</atom:title></feed>')`
$xml = simplexml_load_string('<feed xmlns:atom="http://www.w3.org/2005/Atom"><atom:title>My Feed</atom:title></feed>');
// Declare `$children`, set to `$xml->children('atom', true)`
$children = $xml->children('atom', true);
// Print `$children->title` to the output
echo $children->title;
?>
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. एक XML attribute को array-bracket syntax ($xml[id]) के बजाय -> operator (जैसे $xml->id) से access करने की कोशिश करना, जो है कि SimpleXML attributes को child elements से कैसे अलग करता है।
  2. ऐसे data के लिए एक fragile, deeply chained property-access path लिखना जिसे एक xpath() query से ज़्यादा robustly और flexibly retrieve किया जा सकता।
  3. यह भूल जाना कि xpath() हमेशा matches का एक array return करता है, भले ही आप exactly एक result की उम्मीद करें, single matched element पाने के लिए एक extra [0] चाहिए होते हुए।
चैप्टर सारांश
  • attributes() किसी element के XML attributes को एक iterable collection की तरह return करता है।
  • children() किसी element के direct child elements return करता है, तब उपयोगी जब आप ऐसे data के साथ काम कर रहे हों जिसके exact tag names पहले से न पता हों।
  • xpath($query) document के against एक XPath query चलाता है, XML tree में कहीं भी specific nodes locate करने के लिए एक powerful pattern-matching language।

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.