PHP SimpleXML Get
In this page:
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
// 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";
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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];
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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;
?>
Login to try C/C++/Java/PHP code in the editor
- एक XML attribute को array-bracket syntax ($xml[id]) के बजाय -> operator (जैसे $xml->id) से access करने की कोशिश करना, जो है कि SimpleXML attributes को child elements से कैसे अलग करता है।
- ऐसे data के लिए एक fragile, deeply chained property-access path लिखना जिसे एक xpath() query से ज़्यादा robustly और flexibly retrieve किया जा सकता।
- यह भूल जाना कि 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।
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