If you’re anything like me and spend some time working with JavaScript and the DOM, then it’s highly probable that you’ve already used its “getElementById()” method to access specific elements of a web document. In this particular case, the DOM XML extension also provides PHP developers with a similar method that obviously performs the same task as its client-side counterpart. However, before using this method to access a particular node within an XML document, not only must the element in question have a value for its ID attribute, but this attribute must be of type “ID.” Does that sound a bit confusing? It’s not, actually. Nonetheless, to dissipate any possible doubts about how this method works, below I coded a brand new hands-on example that shows how to access a specific node of an XML document using its ID attribute. Here it is: // example on creating a new DOMDocument object and using the 'getElementById()' to extract a node with a specified ID $elements=array('element1'=>'Element 1','element2'=>'Element 2','element3'=>'Element 3','element4'=>'Element 4','element5'=>'Element 5','element6'=>'Element 6','element7'=>'Element 7','element8'=>'Element 8','element9'=>'Element 9','element10'=>'Element 10'); $dom=new DOMDocument('1.0','iso-8859-1'); $rootElement=$dom->createElement('rootnode',''); // insert the root element into the document $dom->appendChild($rootElement); // insert additional elements into the document foreach($elements as $key=>$value){ $element=$dom->createElement($key,$value); // append attribute to element $element->setAttribute('id',strtolower($value)); // set attribute of type ID $element->setIdAttribute('id',true); // append element to document $rootElement->appendChild($element); } echo 'The node that contains the id 'element 3' is the following: '.$dom->getElementById('element 3')->tagName; /* displays the following The node that contains the id 'element 3' is the following: element3 */ As you can see in the previous example, an attribute of type ID is assigned to each child node of the pertinent XML document via a brand new method, called “setIdAttribute().” After this operation has been completed successfully, a specific element is accessed via the “getElementById()” method. In conclusion, you must bear in mind that before using the “getElemementById()” method directly to get access to a specified XML node, it must contain a valid attribute of type ID. Will you be capable of remembering this basic condition? I bet you will! And finally, you’re free to use all of the code samples included in this tutorial to expand your background in using the DOM XML extension provided by PHP. Final thoughts In this third article of the series, you learned how to use two brand new methods packaged with the DOM XML extension (“createComment()” and “getElementById()”) to insert comments into an XML document and also access specific nodes within the document in question. In the upcoming article of the series, I’ll teach you how to get access to a group of XML nodes via another method that’s used frequently with JavaScript and the DOM. Yes, you guessed right. I’m speaking of the popular “getElementsByTagName()” method. So now that you’ve been warned, you won’t want to miss it!
blog comments powered by Disqus |
|
|
|
|
|
|
|