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

PHP DOM Parser

DOM (Document Object Model) extension किसी XML document को एक पूरे, navigable tree structure में parse करता है -- SimpleXML जैसे spirit में similar, लेकिन एक richer, ज़्यादा standardized API के साथ जो document को modify करके वापस लिखना भी support करता है, इसे तब बेहतर choice बनाते हुए जब आपको XML बनाना या edit करना हो, सिर्फ पढ़ना नहीं।
Syntax
php
$dom = new DOMDocument();
$dom->loadXML($xml_string);   // or $dom->load("file.xml")

$nodes = $dom->getElementsByTagName("tag_name");
$value = $node->getAttribute("attribute_name");

एक XML Document को DOM में Load करना

new DOMDocument() एक नया, खाली DOM document object बनाता है, और इसका load() method (एक file के लिए) या loadXML() method (एक string के लिए) XML content को उस object की tree structure में parse करता है, navigation या modification के लिए ready।

उदाहरण: Loading an XML Document into DOM

php
<?php
// Create a new `DOMDocument` instance, stored in `$dom`
$dom = new DOMDocument();
$dom->loadXML("<catalog><book>PHP Basics</book></catalog>");
// Print `get_class($dom)` to the output
echo get_class($dom);
?>

Tag Name से Elements ढूँढना

getElementsByTagName($tagName) उस tag name से match करने वाले हर element के लिए पूरे document को search करता है, tree में कहीं भी, एक DOMNodeList return करते हुए जिस पर आप loop चला सकते हैं -- "सभी X ढूँढो" के common case के लिए एक XPath query जैसे spirit में similar, लेकिन simpler।

उदाहरण: Finding Elements by Tag Name

php
<?php
// Create a new `DOMDocument` instance, stored in `$dom`
$dom = new DOMDocument();
$dom->loadXML("<catalog><book>A</book><book>B</book></catalog>");
// Declare `$books`, set to `$dom->getElementsByTagName("book")`
$books = $dom->getElementsByTagName("book");
// Loop over `$books`, binding each item to `$book`
foreach ($books as $book) {
    // Print `$book->nodeValue . "\n"` to the output
    echo $book->nodeValue . "\n";
}
?>

Element Attributes पढ़ना

एक DOM element node का getAttribute($name) method सीधे एक specific attribute की value पढ़ता है, जबकि hasAttribute($name) check करता है कि यह exist करता है या नहीं -- DOM API में XML attributes के साथ काम करने के लिए straightforward, explicit methods।

उदाहरण: Reading Element Attributes

php
<?php
// Create a new `DOMDocument` instance, stored in `$dom`
$dom = new DOMDocument();
$dom->loadXML('<book id="1">PHP Basics</book>');
// Declare `$book`, set to `$dom->getElementsByTagName("book")->item(0)`
$book = $dom->getElementsByTagName("book")->item(0);
// Print `$book->getAttribute("id") . "\n"` to the output
echo $book->getAttribute("id") . "\n";
// Print a detailed dump (with types) of `$book->hasAttribute("author")`
var_dump($book->hasAttribute("author"));
?>

DOM से XML बनाना और Modify करना

SimpleXML के limited editing support के उलट, DOM scratch से एक XML document बनाने के लिए methods का एक पूरा set देता है: createElement() एक नया element बनाता है, appendChild() इसे एक parent से attach करता है, और setAttribute() एक attribute add करता है -- आपको programmatically एक document construct या modify करने देते हुए।

उदाहरण: Creating and Modifying XML with DOM

php
<?php
// Create a new `DOMDocument` instance, stored in `$dom`
$dom = new DOMDocument();
// Declare `$book`, set to `$dom->createElement("book", "PHP Basics")`
$book = $dom->createElement("book", "PHP Basics");
$book->setAttribute("id", "1");
$dom->appendChild($book);
// Print `$dom->saveXML()` to the output
echo $dom->saveXML();
?>

Modified Document Save करना

saveXML() पूरे document (या एक specific node) को एक XML string की तरह return करता है, और save($filename) इसे सीधे एक file में लिखता है -- दोनों ही तरीकों में, DOM tree में किए गए changes actual output में सिर्फ तभी effect में आते हैं जब इनमें से कोई method call हो।

उदाहरण: Saving the Modified Document

php
<?php
// Create a new `DOMDocument` instance, stored in `$dom`
$dom = new DOMDocument();
// Declare `$book`, set to `$dom->createElement("book", "PHP Basics")`
$book = $dom->createElement("book", "PHP Basics");
$dom->appendChild($book);
$dom->save("output.xml");
// Print `file_get_contents("output.xml")` to the output
echo file_get_contents("output.xml");
?>
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. एक straightforward read-only parsing task के लिए DOM तक पहुँचना जब SimpleXML simpler और sufficient होगा -- DOM की extra power के साथ noticeably ज़्यादा verbose code आता है।
  2. एक DOM document modify करने के बाद saveXML() (या file के लिए save()) call करना भूल जाना -- in-memory tree में किए गए changes अपने आप कहीं persist नहीं होते।
  3. DOM के अपने node-tree navigation methods (childNodes, getElementsByTagName) को SimpleXML के simpler property-based access से confuse करना -- दोनों APIs interchangeable नहीं हैं।
चैप्टर सारांश
  • DOMDocument::load() या loadXML() एक XML file या string को एक पूरे DOM tree में parse करता है।
  • getElementsByTagName() document में कहीं भी दिए गए tag name वाले सभी elements ढूँढता है।
  • DOM elements बनाना और modify करना support करता है, फिर बदले हुए document को saveXML() या save() से वापस लिखता है।

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.