simplexml_load_string访问标记值

时间:2017-04-13 12:26:41

标签: php xml

我在访问XML标记的值时遇到问题。 这是和XML文件部分:

    <VAST version="3.0">
<Ad id="192347">
<InLine>
<AdSystem version="1.0">Appnext VAST</AdSystem>
<AdTitle>Empire: Four Kingdoms</AdTitle>
<Impression>
https://admin.appnext.com/pView.aspx?b=192347&z=25526&c=125501&tid=vast
</Impression>
<Description>
Become a mighty emperor of the four kingdoms in this award-winning MMO strategy game!
</Description>
<Pricing>"cpi","USD","1.407"</Pricing>
<Error>
https://admin.appnext.com/tp12.aspx?ads_type=VAST&tid=VAST&vid=1&pid=0fa643ba-a3e5-4f01-bf49-89354fbd0562&ref=error&bid=192347&cid=125501
</Error>
<Creatives>
<Creative id="192347">
<Linear>
<Duration>00:00:30.000</Duration>
<VideoClicks>...</VideoClicks>
<TrackingEvents>...</TrackingEvents>
<MediaFiles>
<MediaFile delivery="progressive" type="video/mp4" width="640" height="360" bitrate="500" scalable="true" maintainAspectRatio="true">
<![CDATA[
https://appnext-a.akamaihd.net/banner/video/video-192347-30_o.mp4?rnd=1488986809
]]>
</MediaFile>
<MediaFile delivery="progressive" type="video/mp4" width="320" height="180" bitrate="500" scalable="true" maintainAspectRatio="true">
<![CDATA[
https://appnext-a.akamaihd.net/banner/video/video-192347-30.mp4?rnd=1488986813
]]>
</MediaFile>
</MediaFiles>
</Linear>
</Creative>``

我需要访问MediaFile标记值,但我只获取属性值,如:delivery,type等。 这是我已经做过的代码:

$xml = file_get_contents('https://admin.appnext.com/offerWallApi.aspx?id=0fa643ba-a3e5-4f01-bf49-89354fbd0562&type=vast&vast_ver=3.0&tid=vast&cv=1&duration=all&pbk=test&ip=8.8.8.8', false, $context);
    $response = simplexml_load_string($xml);
    $files = $response->Ad->InLine->Creatives->Creative->Linear->MediaFiles->MediaFile;
    foreach($files as $file) {
        var_dump($file);
    }

PHP响应:

object(SimpleXMLElement)#5(1){[&#34; @ attributes&#34;] =&gt; array(7){[&#34; delivery&#34;] =&gt; string(11)&#34; progressive&#34; [&#34;类型&#34;] =&GT; string(9)&#34; video / mp4&#34; [&#34;宽度&#34;] =&GT;字符串(3)&#34; 640&#34; [&#34;高度&#34;] =&GT; string(3)&#34; 360&#34; [&#34;比特率&#34;] =&GT; string(3)&#34; 500&#34; [&#34;可伸缩&#34;] =&GT; string(4)&#34; true&#34; [&#34; maintainAspectRatio&#34;] =&GT; string(4)&#34; true&#34; object(SimpleXMLElement)#4(1){[&#34; @ attributes&#34;] =&gt; array(7){[&#34; delivery&#34;] =&gt; string(11)&#34; progressive&#34; [&#34;类型&#34;] =&GT; string(9)&#34; video / mp4&#34; [&#34;宽度&#34;] =&GT; string(3)&#34; 320&#34; [&#34;高度&#34;] =&GT; string(3)&#34; 180&#34; [&#34;比特率&#34;] =&GT; string(3)&#34; 500&#34; [&#34;可伸缩&#34;] =&GT; string(4)&#34; true&#34; [&#34; maintainAspectRatio&#34;] =&GT; string(4)&#34; true&#34; }}

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您需要使用显式类型转换来获取CDATA内容:
的var_dump(的(字符串) $文件);

$xml = <<<XML
<VAST version="3.0">
<MediaFiles>
<MediaFile delivery="progressive" type="video/mp4" width="640" height="360" bitrate="500" scalable="true" maintainAspectRatio="true">
<![CDATA[
https://appnext-a.akamaihd.net/banner/video/video-192347-30_o.mp4?rnd=1488986809
]]>
</MediaFile>
<MediaFile delivery="progressive" type="video/mp4" width="320" height="180" bitrate="500" scalable="true" maintainAspectRatio="true">
<![CDATA[
https://appnext-a.akamaihd.net/banner/video/video-192347-30.mp4?rnd=1488986813
]]>
</MediaFile>
</MediaFiles>
</VAST>
XML;

$response = simplexml_load_string($xml);
$files = $response->MediaFiles->MediaFile;
foreach ($files as $file) {
    var_dump((string)$file);
}

https://3v4l.org/Rl6q1#output