Basically theres one <language> which contains lots of <text> elements, I want to retrieve the inner text for a <text> entry with the following function:
function getText($key)
{
$doc = new DOMDocument();
$doc->load($this->_file);
$xpath = new DOMXPath($doc);
$nodelist = $xpath->query("language/text[@key='$key']");
$node = $nodelist->item(0);
// Return inner text from node.
}
Thats what i’ve got so far, except im not sure how to retrieve the inner text for a DomNode ?
If that doesn’t work have a look at the W3 site. They have a UML diagram somewhere showing all of the DOM methods and attributes. Probably something like getText() or somesuch. The model; should be pretty standard accross languages.
All DOM nodes (attributes, “tags”, text nodes, etc.) have a nodeValue property. You should check your code, and the XPath query you’re using is not properly formed (it should being with a /).
I subclassed DomXPath to add a couple of helper functions for when I want to query an XML document and either retrieve only one node, or retrieve the node’s value. This is so I don’t have to deal with the DomNodeList when I know my query is only supposed to match 1 node.
Feel free to use this if you want.
/**
* Extension of DomXPath which adds functions for retrieveing single
* nodes / nodevalues using XPath.
*
* @author Neill Roy <>
* @copyright Neill Roy 2004
* @license GPL
*/
class Pepper_XML_XPath extends DomXPath {
/**
* Constructor.
*
* @access public
* @param object DomDocument
* @return void
*/
public function __construct(DomDocument $domDoc) {
parent::__construct($domDoc);
} // __construct()
/**
* Loads an XML file from the given URI.
*
* @access public
* @static
* @param string Uri of the XML document to load
* @return mixed FALSE if uri can't be loaded, Pepper_XML_XPath if it can.
*/
public static function loadXPathURI($uri) {
$doc = new DomDocument();
if (!$doc->load($uri)) {
return FALSE;
}
return new Pepper_XML_XPath($doc);
} // loadXPathURI()
/**
* This is a helper method for use with XPath queries that will retrieve 1
* (one) matching node's value. If there is more or less that 1 matches the method
* will return FALSE, otherwise the single node's value will be returned.
*
* @access public
* @param string XPath query string
* @return mixed
*/
public function getNodeValue($qry) {
if (!$node = $this->getNode($qry)) {
return FALSE;
}
return $node->nodeValue;
} // getNodeValue()
/**
* This is a helper method for use with XPath queries that will retrieve 1
* (one) matching node. If there is more or less that 1 matches the method
* will return FALSE, otherwise the single node will be returned.
*
* @access public
* @param string XPath query string
* @return mixed
*/
public function getNode($qry) {
// There is no way to retrieve the size of a DOMNodeList
// so we iterate over it and if we only do one iteration return
// the DOMNode in the DOMNodeList otherwise there are
// not enough or to many results.
$i = 0;
foreach ($this->query($qry) as $result) {
$i++;
$last = $result;
if ($i == 2) {
return FALSE;
}
}
if ($i == 1) {
return $last;
}
} // getNode()
} // Pepper_XML_XPath
Sorry for the late reply. If you haven’t solved your problem yet:
What version of PHP are you using?
Ask your tech support if you’re not sure or use [fphp]phpversion[/fphp].
You could also try asking in the php.xml.dev newsgroup - give them all the info you gave above along with the version number and I’m sure they’ll be of more help.