1

I have the following XML-Structure:

<?xml version="1.0" encoding="utf-8"?>
<psc:chapters version="1.2" xmlns:psc="http://podlove.org/simple-chapters">
    <psc:chapter start="00:00:12.135" title="Begrüßung" />
    <psc:chapter start="00:00:20.135" title="Faktencheck: Keine Werftführungen vor 2017"  />
    <psc:chapter start="00:02:12.135" title="Sea Life Timmendorfer Strand"" />

I need to get the title and start attribute. I already managed to get to the elements:

$feed_url="http://example.com/feed.psc";
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
$chapters=$x->children('psc', true);

foreach ($chapters as $chapter) {
    $unter=$chapter->children();
    print_r($unter);
}

The output is something like:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [start] => 00:00:12.135
            [title] => Begrüßung
        )    
)

When I now follow the answers here on SO to multiple questions to get the @attributes:

echo $unter->attributes()["start"];

I just receive an empty result.

(Update) print_r($unter->attributes()) returns an empty object:

SimpleXMLElement Object
(
)
1
  • 2
    You already got the right answer, but to clarify: The attributes are not in a namespace - only attributes with a prefix can be in a namespace (unlike element nodes). Additionally I suggest using the actual namespace, not the alias/prefix: $x->children('http://podlove.org/simple-chapters'); Commented Nov 14, 2016 at 15:29

3 Answers 3

2

You need to get your attributes from chapter.

foreach ($chapters as $chapter) {
    // You can directly read them 
    echo $chapter->attributes()->{'title'}

    // or you can loop them
    foreach ($chapter->attributes() as $key => $value) {
        echo $key . " : " . $value;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Your xml format was wrong (ending chapters tag). I modified your xml and php codes (reading chapter tag) like following format. Now its working perfect!

XML String:

<?xml version="1.0" encoding="UTF-8"?>
<psc:chapters xmlns:psc="http://podlove.org/simple-chapters" version="1.2">
   <psc:chapter start="00:00:12.135" title="Begrüßung" />
   <psc:chapter start="00:00:20.135" title="Faktencheck: Keine Werftführungen vor 2017" />
   <psc:chapter start="00:02:12.135" title="Sea Life Timmendorfer Strand" />
</psc:chapters>

PHP Codes:

$x = simplexml_load_string($xmlString);
$chapters=$x->children('psc', true);

foreach ($chapters->chapter as $chapter) {
    echo $chapter->attributes()->{'start'};
}

1 Comment

The missing closing tag was just my mistake here, because I did not want to paste the whole 100 lines of the xml here. Your code was right, though, i simply used the wrong object ($unter instead of $chapter) like mim found out.
0

You have to use the element like an object, not an array:

echo $unter->attributes()->start;

More info in: http://php.net/simplexmlelement.attributes

1 Comment

Did not work either. $unter->attributes() seems to be an empty object

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.