php - Simplexml: Select element by id a echo a certain child element -


i'd need advise simplexml xpath. have master xml-file entire website this:

<content> <item id="001">     <title>video 1</title>     <video>         <file>video001.mp4</file>         <dur>01:20</dur>     </video> </item> <item id="2">     <title>video 2</title>     <video>         <file>video002.mp4</file>         **<dur>03:53</dur>**     </video> </item> </content> 

now want echo duration of video of item id "2" string "03:53".

seems simples use of simplexml, tons of tutorials out there cover loops , selection array number. want access parent element id grab string of child element of it.

thanks in advance! runa

you may use xpath purpose: http://php.net/manual/en/simplexmlelement.xpath.php

you code should (more or less):

$xml = simplexml_load_file('path/to/xml/file.xml'); $result = $xml->xpath('/content/item[@id=2]/video/dur/text()'); $result = $result[0]; echo $result; 

here phpfiddle http://phpfiddle.org/main/code/9hc-yqi

probably should cast xpath result on string i'm not sure. tested xpath part of code inside http://www.xpathtester.com/ , works fine.


Comments

Popular posts from this blog

design - Custom Styling Qt Quick Controls -

Unable to remove the www from url on https using .htaccess -