无效标签上的php简单xml解析问题 [英] php simple xml parse problem on invalid tags

查看:26
本文介绍了无效标签上的php简单xml解析问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

how can i parse element like this example

<?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 中未定义视图中的命名空间前缀角色

在/home/sweb/www/tmp/sxml.php 第 35 行

推荐答案

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

XML parsers rely on the fact documents have to be strictly valid, and yours is not well-formed. It needs a namespace URI for actor. The root element should be along the lines of this:

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

只要您没有它,任何兼容的 XML 解析器都不会接受您的文档.

As long as you don't have it, no compliant XML parser will accept your document.

这篇关于无效标签上的php简单xml解析问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆