1

how can i get the attribute values for seconds using php:

<yt:duration seconds='12445'/>

this is what i have done so far:

        $xmlstr  = file_get_contents($xml);
    $xml_content = new SimpleXMLElement($xmlstr); 
    echo $xml_content->xpath('//yt:duration')->attributes;
    print_r($xml_content->xpath('//yt:duration'));
    echo $xml_content->xpath('//yt:duration')->attributes()->seconds;

which displays these messages:

Notice: Trying to get property of non-object in C:\xampp\htdocs\ytm\xmlTest.php on line 22
Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [seconds] => 12445 ) ) ) 
Notice: Trying to get property of non-object in C:\xampp\htdocs\ytm\xmlTest.php on line 24

Notice: Trying to get property of non-object in C:\xampp\htdocs\ytm\xmlTest.php on line 24

1 Answer 1

2

This:

$xml_content->xpath('//yt:duration')

Returns an array (as your print_r() will tell you). You'll have to grab the first element (at index 0) in order to work with the SimpleXMLElement that corresponds to the <yt:duration> node:

$list = $xml_content->xpath('//yt:duration');
$node = $list[0];

Then, you can get the attibutes with attributes():

$attributes = $node->attributes();
echo $attributes['seconds']; // Not 100% sure on this, might be $attributes->seconds
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.