php无效标签上的简单xml解析问题

时间:2010-08-08 06:56:12

标签: php xml

我如何解析像这个例子的元素

<?php

$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
 <movie>
  <title>PHP: Behind the Parser</title>
  <characters>
   <character>
    <name>Ms. Coder</name>
    <actor>Onlivia Actora</actor>
    <actor:view id='44' />
   </character>
   <character>
    <name>Mr. Coder</name>
    <actor>El Act&#211;r</actor>
    <actor:view id='49' />
   </character>
  </characters>
  <plot>
   So, this language. It's like, a programming language. Or is it a
   scripting language? All is revealed in this thrilling horror spoof
   of a documentary.
  </plot>
  <great-lines>
   <line>PHP solves all my web problems</line>
  </great-lines>
  <rating type="thumbs">7</rating>
  <rating type="stars">5</rating>
 </movie>
</movies>
XML;


 $simpleXml = new SimpleXMLElement($xmlstr);

 var_dump((array) $simpleXml);

名称空间错误:第35行/home/sweb/www/tmp/sxml.php中未定义视图上的名称空间前缀actor

第35行/home/sweb/www/tmp/sxml.php中的

1 个答案:

答案 0 :(得分:3)

XML解析器依赖于事实文档必须严格有效,而您的文档格式不正确。它需要actor的名称空间URI。根元素应该是这样的:

<movies xmlns:actor="http://yoururl.com/">

只要您没有,没有合规的XML解析器会接受您的文档。