DOMXML and XPATH

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
evlich
Forum Newbie
Posts: 2
Joined: Sun Mar 06, 2005 9:22 pm

DOMXML and XPATH

Post by evlich »

Hello, I have been working on trying to find good documentation all day and I can't really find anything that is helping me. I have an XML file that contains data about parts of my site and I am trying to query for them, but I can't get XPATH to work with DOMXML. I tried the simple xml extension but I had some problems with that too. Here is what I got:

Code: Select all

<?php

require_once('ASection.php');

class Site extends ASection &#123;
	private $cache_;
	private $dom_;
	private $xpath_;
	
	public function __construct($fileName) &#123;
		$this->dom_ = DOMDocument::load($fileName);
		$this->xpath = $this->dom_->xpath_new_context();
		$this->cache_ = array();
	&#125;	
	
	
	public function getTitle() &#123;
		if( isset($this->cache_&#1111;'title']) ) 
			return $this->cache_&#1111;'title'];
		return $this->cache_&#1111;'title'] = $this->dom_->xpath('/section/header/title/text()');;
	&#125;
	// This could have markup
	public function getBlurb() &#123;
		if( isset($this->cache_&#1111;'blurb']) ) 
			return $this->cache_&#1111;'blurb'];
		return $this->cache_&#1111;'blurb'] = $this->dom_->xpath('/section/header/blurb');
	&#125;
	// This does have markup
	public function getContents() &#123;
		if( isset($this->cache_&#1111;'contents']) ) 
			return $this->cache_&#1111;'contents'];
		return $this->cache_&#1111;'contents'] = $this->dom_->xpath('/section/contents');
	&#125;
	// This does have markup
	public function getMenu() &#123;
		if( isset($this->cache_&#1111;'menu']) ) 
			return $this->cache_&#1111;'menu'];
		return $this->cache_&#1111;'menu'] = $this->dom_->xpath('/section/menu');
	&#125;
	public function getNews() &#123;
		if( isset($this->cache_&#1111;'news']) )
			return $this->cache_&#1111;'news'];
		return $this->cache_&#1111;'news'] = $this->dom_->xpath('/section/news');
	&#125;
&#125;

?>

<html>
<body>
<?php
	//require_once('Site.php');
	
	
	
	$site = new Site('SiteTest.xml');
	print_r($site->getTitle());
	//print_r($site->getBlurb());
	//print_r($site->getContents());
	
	
?>
</body>
</html>
I get an error that xpath_new_context() is an invalid function. I found that at http://us3.php.net/manual/en/function.x ... ontext.php, but they used a different line for opening the document; however, that line fails for me saying that there is no such function. I am using PHP 5.0.3 (downloaded it today). Any ideas as to what I am doing wrong or what I need to do to fix my problem? Thanks a bunch.

~evlich
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

please notice that you are using the DOM extension....

and the function you try to use is from the DOM XML extension....
evlich
Forum Newbie
Posts: 2
Joined: Sun Mar 06, 2005 9:22 pm

Post by evlich »

So, how do I use the DOMXML extension? Do I need to install it as a module? When I do domxml_open_file() it gives me an error saying invalid function.
Thanks
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

as it's explained in the manual, under the dom xml extension...

you need to install the extension ;)
with the windows version (the zip not the installer) it was as easy as adding: extension=php_domxml.dll to the php.ini file and restarting apache.
Post Reply