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

PHP XML Handling

XML labeled tags से information लिखने का एक और तरीका है, हर item को एक नाम वाले box में डालने जैसा। PHP इन boxes को पढ़ सकता है और जो चाहिए वह निकाल सकता है।
Syntax
php
$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
<?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);
?>

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
<?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;
?>

XML Elements Modify करना

क्योंकि SimpleXML elements document में live references जैसे behave करते हैं, $element->title को एक नई value assign करना असल में underlying XML structure को update कर देता है, वापस save होने के लिए ready।

उदाहरण: Modifying XML Elements

php
<?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();
?>

SimpleXML से XML बनाना

addChild() और addAttribute() आपको scratch से programmatically XML documents बनाने देते हैं, एक मौजूदा SimpleXMLElement में नए nested elements और attributes append करते हुए।

उदाहरण: Creating XML with SimpleXML

php
<?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();
?>

XML Lists में Loop चलाना

जब किसी XML document में repeated elements हों (जैसे कई <item> tags), एक standard foreach loop उन पर cleanly iterate करता है, आपको हर एक को exactly एक array पर loop चलाने जैसा process करने देते हुए।

उदाहरण: Looping Through XML Lists

php
<?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";
}
?>
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. यह check न करना कि simplexml_load_string ने एक object return किया, ताकि invalid XML एक confusing error दे।
  2. -> के बजाय array syntax [attr] से एक attribute पढ़ना।
  3. हाथ से XML बनाते समय & जैसे special characters escape न करना।

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.