简体   繁体   中英

php dom xml get node value

I am working on a project, which works with .xml files. I encountered a problem. I use simplexml for reading and changing values of nodes. And As I need to add and remove nodes I have to use DOM as well. I read through DOM documentation over on the php.net site and I understand the concept and how it works, but.

I load external .xml file. to $xmlpath="./menu.xml"; the file is way bigger, but important part is:

<?xml version="1.0" encoding="utf-8"?>
<menu>
    <den>
        <nazev>pondeli</nazev>
    </den>
</menu>

My php code is:

$dom=new DOMDocument(); // iniciate DOM
$dom->load($xmlpath); // load xml file
$path = new DOMXPath($dom); // prepare path
echo $path->den[0]->nazev->nodeValue;

I just simply cannot wrap my head around this, why this doesn't work. I am certain I might be doing something wrong, but I have no idea what it is. I know I can just use simpleXML for doing this, but I wanted to use this, to teach myself use DOM. Thank you all in advance.

Using the item functions you can get the element you are looking for

$path->item(0)->nodeValue;

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM