douxing6434 2017-04-13 12:26
浏览 54
已采纳

simplexml_load_string访问标记值

I have problem with accessing value of XML tag. Here is and XML file part:

    <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>``

I need to access MediaFile tag value but i only get atributes values like: delivery, type etc. Here is code i already have made:

$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);
    }

And PHP response:

object(SimpleXMLElement)#5 (1) { ["@attributes"]=> array(7) { ["delivery"]=> string(11) "progressive" ["type"]=> string(9) "video/mp4" ["width"]=> string(3) "640" ["height"]=> string(3) "360" ["bitrate"]=> string(3) "500" ["scalable"]=> string(4) "true" ["maintainAspectRatio"]=> string(4) "true" } } object(SimpleXMLElement)#4 (1) { ["@attributes"]=> array(7) { ["delivery"]=> string(11) "progressive" ["type"]=> string(9) "video/mp4" ["width"]=> string(3) "320" ["height"]=> string(3) "180" ["bitrate"]=> string(3) "500" ["scalable"]=> string(4) "true" ["maintainAspectRatio"]=> string(4) "true" } }

Thanks in advance for any help.

  • 写回答

1条回答 默认 最新

  • dongzha5934 2017-04-13 15:21
    关注

    You need to use an explicit type conversion to get CDATA content:
    var_dump((string)$file);

    $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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题