Home arrow PHP arrow Page 4 - Inserting Comments and Accessing Nodes with the DOM XML Extension in PHP 5

Accessing XML nodes by their ID attribute - PHP

Welcome to the third chapter of the series “A quick overview of the DOM XML extension in PHP 5.” As the article’s title suggests, this series walks you through using the most relevant methods that come packaged with this powerful XML-related PHP extension to perform specific operations on XML documents by using the API of the Document Object Model.

TABLE OF CONTENTS:
  1. Inserting Comments and Accessing Nodes with the DOM XML Extension in PHP 5
  2. Reviewing how to insert attributes and CDATA nodes with the DOM XML library
  3. Appending comment nodes to a given XML document
  4. Accessing XML nodes by their ID attribute
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
March 11, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: