我有以下XML结构:
<?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"" />
我需要获取标题和开始属性。 我已经设法了解元素:
$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);
}
输出类似于:
SimpleXMLElement Object
(
[@attributes] => Array
(
[start] => 00:00:12.135
[title] => Begrüßung
)
)
当我现在按照这里的答案回答多个问题来获取@attributes:
echo $unter->attributes()["start"];
我收到一个空的结果。
(更新)
print_r($unter->attributes())
返回一个空对象:
SimpleXMLElement Object
(
)
答案 0 :(得分:2)
您需要从章节中获取属性。
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;
}
}
答案 1 :(得分:1)
您的xml格式错误(结束章节标记)。我修改了你的xml和php代码(阅读章节标签),如下面的格式。现在它的工作完美!
XML字符串:
<?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代码:
$x = simplexml_load_string($xmlString);
$chapters=$x->children('psc', true);
foreach ($chapters->chapter as $chapter) {
echo $chapter->attributes()->{'start'};
}
答案 2 :(得分:0)
你必须像对象一样使用元素,而不是数组:
echo $unter->attributes()->start;