note 90228 added to domdocument.getelementbyid

From: Date: Sat, 11 Apr 2009 07:41:49 +0000
Subject: note 90228 added to domdocument.getelementbyid
Groups: php.notes 
Request: Send a blank email to php-notes+get-153075@lists.php.net to get a copy of this message
From my experience, getElementById seem to work fine without any setups if you have loaded a HTML document. But in order for getElementById to work with a simple XML document that you've "constructed", you have to set up the id with "xml:" prefix and use setIdAttribute on the element you created or it won't work. See following example, hope this will save someone's frustration. If you have loaded the xml file, then all you have to make sure is the ID has a xml: prefix for the attribute. But if you start to append the XML document, don't forget to setIdAttribute on the id name or those elements or getElementById will return null when you try to find them. <?php /* test.xml <?xml version="1.0" encoding="utf-8"?> <root> <child xml:id="id_xxxxxx" status="partial"> <sub_child>Some Data</sub_child> </child> </root> */ $xmlDom = new DOMDocument('1.0', 'utf-8'); $xmlDom->formatOutput = true; // we want a nice output // create a root $eltRoot = $xmlDom->createElement("root"); $xmlDom->appendChild($eltRoot); $eltChild = $xmlDom->createElement("child"); $eltRoot->appendChild($eltChild); // add a id attribute $attr = $xmlDom->createAttribute("xml:id"); // needs xml prefix or getElementById won't work $eltChild->appendChild($attr); /// create the text node and append to the created element $tNode = $xmlDom->createTextNode("id_8120528"); $attr->appendChild($tNode); $eltChild->setIdAttribute("xml:id", true); // VERY IMPORT or getElementById won't work // add a id attribute $attr = $xmlDom->createAttribute("status"); $eltChild->appendChild($attr); /// create the text node and append to the created element $tNode = $xmlDom->createTextNode("partial"); $attr->appendChild($tNode); // add a subchild $eltSub = $xmlDom->createElement("sub_child"); $eltChild->appendChild($eltSub); $tNode = $xmlDom->createTextNode("Some Data"); $eltSub->appendChild($tNode); $id = null; $id = $xmlDom->getElementById("id_8120528"); assert ($id != null); $strId = $id->getAttribute("xml:id"); // bug? empty $strStatus = $id->getAttribute("status"); // this works! assert ($id !=null); $xmlDom->save("./_data/test.xml"); $xmlDom->load("./_data/test.xml"); // reloading fixes the problem $nodeRoot = $xmlDom->getElementsByTagName("root"); if ($nodeRoot->length > 0) { $eltRoot = $nodeRoot->item(0); } assert($eltRoot != null); $id = null; $id = $xmlDom->getElementById("id_8120528"); assert ($id != null); $strId = $id->getAttribute("xml:id"); // this works now! $strStatus = $id->getAttribute("status"); // this works! ?> ---- Server IP: 208.69.120.35 Probable Submitter: 67.182.19.186 ---- Manual Page -- http://www.php.net/manual/en/domdocument.getelementbyid.php Edit -- https://master.php.net/note/edit/90228 Del: integrated -- https://master.php.net/note/delete/90228/integrated Del: useless -- https://master.php.net/note/delete/90228/useless Del: bad code -- https://master.php.net/note/delete/90228/bad+code Del: spam -- https://master.php.net/note/delete/90228/spam Del: non-english -- https://master.php.net/note/delete/90228/non-english Del: in docs -- https://master.php.net/note/delete/90228/in+docs Del: other reasons-- https://master.php.net/note/delete/90228 Reject -- https://master.php.net/note/reject/90228 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#153075) next »