Financial Calculator

Calculate loans, SIP, PPF, NPS & more!

Financial Calculator

Calculate loans, SIP, PPF, NPS & more!

PHP XML DOM

PHP

PHP XML DOM (DOMDocument Parser)

DOMDocument PHP का advanced XML parser है, जो XML को एक complete tree structure (DOM – Document Object Model) में load करता है। DOM की मदद से हम XML को पढ़, बदल, nodes जोड़, हटाकर और नई XML files बना सकते हैं।

  • XML को full tree structure में load करता है।
  • Create, edit, delete, और rearrange करने की सुविधा देता है।
  • Small/medium XML files के लिए powerful और flexible है।

DOMDocument Functions

  • load() – XML file load करता है।
  • loadXML() – XML string load करता है।
  • getElementsByTagName() – specific tag elements को प्राप्त करता है।
  • createElement() – नया element बनाता है।
  • appendChild() – DOM में new node जोड़ता है।

XML Load और Read करना

पहले XML को string या file से load करते हैं और फिर nodes को access करते हैं।

उदाहरण: DOMDocument से XML पढ़ना
<?php
$xml = " <users> <user>Rahul</user> <user>Amit</user> </users> ";

$dom = new DOMDocument();
$dom->loadXML($xml);

$users = $dom->getElementsByTagName("user");
foreach ($users as $u) {
  echo $u->nodeValue . "<br>";
}

?>
Output
Rahul
Amit

XML File Load करना

अगर XML data किसी external file में है, तो load() function का उपयोग करते हैं।

उदाहरण: XML File Load करना
<?php
$dom = new DOMDocument();
$dom->load("users.xml");

$items = $dom->getElementsByTagName("item");
foreach ($items as $it) {
  echo $it->nodeValue . "<br>";
}

?>

XML Elements Create करना

DOMDocument का सबसे powerful feature है — नए XML elements create करना और उन्हें XML tree में जोड़ना।

उदाहरण: नया XML Element Create और Append करना
<?php
$dom = new DOMDocument();
$dom->loadXML("<fruits></fruits>");

$newFruit = $dom->createElement("fruit", "Apple");
$dom->documentElement->appendChild($newFruit);

echo $dom->saveXML();

?>
Output
<fruits><fruit>Apple</fruit></fruits>

Attributes Get और Set करना

DOMDocument में attribute को get और set करना बहुत आसान है।

उदाहरण: Get/Set Attributes
<?php
$dom = new DOMDocument();
$dom->loadXML(" <user id='101' city='Delhi'>Amit</user> ");

$user = $dom->getElementsByTagName("user")[0];

echo "ID: " . $user->getAttribute("id") . "<br>";
$user->setAttribute("city", "Mumbai");

echo $dom->saveXML();
?>

XML Nodes Remove करना

DOM में किसी भी element को remove करना बहुत आसान है।

उदाहरण: Remove Node
<?php
$dom = new DOMDocument();
$dom->loadXML(" <items> <item>A</item> <item>B</item> </items> ");

$itemList = $dom->getElementsByTagName("item");
$toRemove = $itemList[0];
$toRemove->parentNode->removeChild($toRemove);

echo $dom->saveXML();

?>
Output
<items><item>B</item></items>
Article By: Brajlal Prasad
Created on: 03 Nov 2025  10  Views
 Print Article
Report Error

If you want to report an error, or any suggestion please send us an email to [email protected]

Financial Calculator

Financial Calculator

Take control of your finances with our powerful Financial Calculator app—calculate loans, SIP, PPF, NPS and more all in one place!

Play Store