PHP DOM Parser
In this page:
$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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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"));
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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");
?>
Login to try C/C++/Java/PHP code in the editor
- एक straightforward read-only parsing task के लिए DOM तक पहुँचना जब SimpleXML simpler और sufficient होगा -- DOM की extra power के साथ noticeably ज़्यादा verbose code आता है।
- एक DOM document modify करने के बाद saveXML() (या file के लिए save()) call करना भूल जाना -- in-memory tree में किए गए changes अपने आप कहीं persist नहीं होते।
- 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() से वापस लिखता है।
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