PHP XML Handling
In this page:
$xml = simplexml_load_string($xml_string);
$xml = simplexml_load_file("file.xml");
echo $xml->element_name; // read an element
$xml->element_name = "new value";
XML क्या है?
XML structured data को nested tags की तरह represent करता है, HTML से spirit में similar लेकिन display के बजाय data exchange के लिए designed; PHP का SimpleXML extension एक XML document को एक easy-to-navigate object tree में बदल देता है।
उदाहरण: What is XML?
<?php
// Declare `$xml`, set to `simplexml_load_string("<book><title>PHP Basics</title></book>")`
$xml = simplexml_load_string("<book><title>PHP Basics</title></book>");
// Print `get_class($xml)` to the output
echo get_class($xml);
?>
Login to try C/C++/Java/PHP code in the editor
XML Elements पढ़ना
simplexml_load_string() या simplexml_load_file() से load होने के बाद, XML elements regular object properties की तरह accessible हो जाते हैं, इसलिए <book><title>...</title></book> बिना manual parsing के $book->title बन जाता है।
उदाहरण: Reading XML Elements
<?php
// Declare `$xml`, set to `simplexml_load_string("<book><title>PHP Basics</title></book>")`
$xml = simplexml_load_string("<book><title>PHP Basics</title></book>");
// Print `$xml->title` to the output
echo $xml->title;
?>
Login to try C/C++/Java/PHP code in the editor
XML Elements Modify करना
क्योंकि SimpleXML elements document में live references जैसे behave करते हैं, $element->title को एक नई value assign करना असल में underlying XML structure को update कर देता है, वापस save होने के लिए ready।
उदाहरण: Modifying XML Elements
<?php
// Declare `$xml`, set to `simplexml_load_string("<book><title>Old Title</title></book>")`
$xml = simplexml_load_string("<book><title>Old Title</title></book>");
$xml->title = "New Title";
// Print `$xml->asXML()` to the output
echo $xml->asXML();
?>
Login to try C/C++/Java/PHP code in the editor
SimpleXML से XML बनाना
addChild() और addAttribute() आपको scratch से programmatically XML documents बनाने देते हैं, एक मौजूदा SimpleXMLElement में नए nested elements और attributes append करते हुए।
उदाहरण: Creating XML with SimpleXML
<?php
// Create a new `SimpleXMLElement` instance with "<book></book>", stored in `$xml`
$xml = new SimpleXMLElement("<book></book>");
$xml->addChild("title", "PHP Basics");
$xml->addAttribute("id", "1");
// Print `$xml->asXML()` to the output
echo $xml->asXML();
?>
Login to try C/C++/Java/PHP code in the editor
XML Lists में Loop चलाना
जब किसी XML document में repeated elements हों (जैसे कई <item> tags), एक standard foreach loop उन पर cleanly iterate करता है, आपको हर एक को exactly एक array पर loop चलाने जैसा process करने देते हुए।
उदाहरण: Looping Through XML Lists
<?php
// Declare `$xml`, set to `simplexml_load_string("<items><item>A</item><item>B</item></items>")`
$xml = simplexml_load_string("<items><item>A</item><item>B</item></items>");
// 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
- यह check न करना कि
simplexml_load_stringने एक object return किया, ताकि invalid XML एक confusing error दे। ->के बजाय array syntax[attr]से एक attribute पढ़ना।- हाथ से XML बनाते समय
&जैसे special characters escape न करना।
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