【问题标题】:Simplexml_load_string() removing a lot of contentSimplexml_load_string() 删除大量内容
【发布时间】:2011-09-29 22:29:31
【问题描述】:

Simplexml_load_string 正在删除大量传入其中的内容。

var_dump($cleaned);
<item>
   <title><![CDATA[Honda Motor Company: Motorcycles to the Civic and Accord]]></title>
   <link>http://www.link.com/index.php?id=9987</link>
   <description><![CDATA[Though this company is famous for its cars, it actually started as a motorcycle manufacturer. http://www.link.com takes a look at the history of the Honda Motor Company, from its popular motorcycles to the Civic and the Accord.]]>    </description>
   <pubDate>Fri, 23 Sep 2011 10:55:50 -0400</pubDate>
   <guid>https://link.cloudfront.net/581575d3053ac1a578065c10f2bac1be.mp4</guid>
   <media:title><![CDATA[Honda Motor Company: Motorcycles to the Civic and Accord]]>    </media:title>
<media:description><![CDATA[Though this company is famous for its cars, it actually started as a motorcycle manufacturer. http://www.link.com takes a look at the history of the Honda Motor Company, from its popular motorcycles to the Civic and the Accord.]]></media:description>

<media:keywords><![CDATA[Honda Motor Company, Honda, motorcycles, Japan, Civic, Accord, Formula 1, F1, Acura, fuel efficiency, hybrid, ASIMO, robot, aviation, Dream D, Isle of Man races, Super Cub, auto, automotive, automobile, automaker, cars, driving, history, Soichiro Honda]]></media:keywords>
<media:tags><![CDATA[Asian History, Automakers, Company Profiles, Honda Motor Company, Honda, motorcycles, Japan, Civic, Accord, Formula 1, F1, Acura, fuel efficiency, hybrid, robot, aviation, auto, automotive, automobile, automaker, cars, driving, history, Soichiro Honda]]></media:tags>
<media:category><![CDATA[/Video/Auto/Reviews and Profiles]]></media:category>
<media:thumbnail url="http://link.cloudfront.net/images/linkthumbs/A-RP-Honda-480i60_100x57.jpg"/>   
<media:content url="https://link.cloudfront.net/581575d3053ac1a578065c10f2bac1be.mp4"  type="video/mp4" >
  <media:thumbnail url="http://link.cloudfront.net/images/linkthumbs/A-RP-Honda-480i60_100x57.jpg"/>
  <media:player url="http://www.link.com/index.php?id=9987"><![CDATA[<embed src="https://link.cloudfront.net/581575d3053ac1a578065c10f2bac1be.mp4" type="application/x-shockwave-flash" wmode="transparent" width="480" height="270"></embed>]]></media:player>
  <media:text_content><![CDATA[Long description.]]></media:text_content>
  <media:filename><![CDATA[]]></media:filename>

  <media:author><![CDATA[Rebecca Brayton]]></media:author>
  <media:bliptv_thumbnail><![CDATA[A-RP-Honda-480i60_480x270.jpg]]></media:bliptv_thumbnail>
  <media:preference_rate><![CDATA[4]]></media:preference_rate>
</media:content>

运行下面的代码后

$xml = simplexml_load_string(utf8_encode($cleaned), 'SimpleXMLElement', LIBXML_NOCDATA);

返回

["item"]=>
array(100) {
  [0]=>
  object(SimpleXMLElement)#4 (5) {
    ["title"]=>
    string(56) "Honda Motor Company: Motorcycles to the Civic and Accord"
    ["link"]=>
    string(42) "http://www.link.com/index.php?id=9987"
    ["description"]=>
    string(232) "long description."
    ["pubDate"]=>
    string(31) "Fri, 23 Sep 2011 10:55:50 -0400"
    ["guid"]=>
    string(74) "https://link.cloudfront.net/581575d3053ac1a578065c10f2bac1be.mp4"
  }

在此之前没有运行其他代码,我似乎无法弄清楚为什么会发生这种情况。有人知道这背后的原因,是因为 utf-8 编码吗?

【问题讨论】:

    标签: php xml utf-8 simplexml


    【解决方案1】:

    您需要使用"media" 命名空间调用SimpleXMLElement-&gt;children() 以获取属于&lt;media:xxx&gt; 格式的部分,或者将$ns 参数传递给simplexml_load_string() 以指定要加载的命名空间:

    $xml = simplexml_load_string(utf8_encode($cleaned), 'SimpleXMLElement', LIBXML_NOCDATA, 'media');
    

    http://www.php.net/manual/en/function.simplexml-load-string.php

    http://www.php.net/manual/en/simplexmlelement.children.php

    【讨论】:

    • 我已经尝试过你的方法,看起来它应该可以工作,但它返回的是空的,例如 object(SimpleXMLElement)#1 (0) { }
    • 可能是因为顶级 item 不在 media 命名空间中 - 因此我建议使用 item-&gt;children('media')
    • 感谢 Amber 的帮助 :) 您的帖子为我指明了正确的方向。关注了这个博客,它工作了blog.sherifmansour.com/?p=302
    猜你喜欢
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 2018-06-07
    • 1970-01-01
    • 2020-01-06
    • 2013-07-05
    相关资源
    最近更新 更多