Jump to content

[SOLVED] DOMDocument and DOMNodeList


Vebut

Recommended Posts

Hello,

I'm working on a selector class and would like to select elements and nodes based on attributes and values. I know how to retrieve the nodes and everything is working as it should, but I'm stuck at the point where I'd like a new DOMNodeList to work with:

 

<?php
/**
 * Select node/element by attribute and its value.
 * 
 * @param string $attr
 * @param string $value
 * @return object
 */
public function elementsByAttr ($attr, $value)
{
	$this->nodes = $this->getElementsByTagName('*');
	foreach ($this->nodes as $id => $node)
	{
		if ($node->getAttribute($attr) == $value)
		{
			// New Dom node list.
		}
	}
	return $this;
}
?>

 

Is there a way to manually create a DOMNodeList or alter the existing one in $this->nodes (by removing the nodes that does not match the criteria)?

 

Thanks,

Daniel

Link to comment
https://forums.phpfreaks.com/topic/167501-solved-domdocument-and-domnodelist/
Share on other sites

Ok, instead of limiting me to the domdocument functions I included the DOMXpath class, solution:

<?php
/**
 * Select node/element by attribute and its value.
 * 
 * @param string $attr
 * @param string $value
 * @return bool|object
 */
public function elementsByAttr ($attr, $value)
{
	$xpath = new DOMXPath($this);
	return $this->nodes($xpath->query("//*[@$attr='$value']"));
}
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.