I have this xml structure:
<?xml version="1.0" ?>
<log>
<path>/dir/file</path>
<name>log_</name>
</log>
this is my function:
function getNode($node)
{
$dom = new DOMDocument();
$dom->load('config.xml');
$value = $dom->getElementsByTagName($node);
foreach($value as $val)
{
$ints = $val->getElementsByTagName($node);
$intVal = $ints->item(0)->nodeValue;
echo $intVal;
}
}
how I can get the value of path
node? I pass the node as parameter like path
. But the code doesn't enter in the foreach. What I did wrong? I'm waiting the /dir/file
result
I hope, this will help:
function getNode($node)
{
$dom = new DOMDocument();
$dom->load('config.xml');
$value = $dom->getElementsByTagName($node);
if ($value->length > 0) {
$intVal = $value->item(0)->nodeValue;
echo $intVal;
}
}
The explanation is very simple - there is detailed manual about using DOMDocument class, provided on php.net .
You can pass the node name 'path' to the function getNode
. The function getElementsByTagName takes a string as a parameter and will return a DOMNodeList which you can loop using a foreach
.
For example:
function getNode($node)
{
$dom = new DOMDocument();
$dom->load('config.xml');
$value = $dom->getElementsByTagName($node);
foreach($value as $val)
{
$path = $val->nodeValue;
echo $path;
}
}
getNode('path');
Will result in:
/dir/file
You could use SimpleXml instead.
simpleXml example - php.net
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.