php simplexml_load_file - 无法在print_r中看到 [英] php simplexml_load_file - not able to see in print_r

查看:24
本文介绍了php simplexml_load_file - 无法在print_r中看到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 PHP 中 simplexml parser 的新手.我已经运行了我找到的示例,并且它们的工作原理与广告一样.不过,我无法让它与我的程序一起使用.我已经在这里搜索了几个小时.

I am new to simplexml parser in PHP. I have run the examples that I have found and they work as advertised. I can't get it to work with my program though. I have searched in here for hours.

我有一个名为 core.xml 的 XML 文件(注意节点标签中有冒号):

I have an XML file called core.xml (note that the node tags have colons in):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <dc:title>Lecture 3</dc:title>
    <dc:creator>John Brown &amp; Greg Smith</dc:creator>
    <cp:lastModifiedBy>Greg Smith</cp:lastModifiedBy>
    <cp:revision>165</cp:revision>
    <dcterms:created xsi:type="dcterms:W3CDTF">2010-02-19T04:37:55Z</dcterms:created>
    <dcterms:modified xsi:type="dcterms:W3CDTF">2014-01-30T02:41:30Z</dcterms:modified>

</cp:coreProperties>

我使用以下代码将其加载到解析器中:

I use following code to load it into the parser:

if (file_exists('core.xml')){

    echo 'file exists <br>';
    $xml_core = simplexml_load_file('core.xml');

    print_r($xml_core);
} 
else 
{    
   exit('Failed to open core.xml.');  
}

该文件存在,但我在 print_r 中得到的只是:

The file exists but all I get at the print_r is:

file exists    
SimpleXMLElement Object ( ) 

我如何访问节点?我必须使用的其他 XML 文件有很多深度.

How do I access the nodes? Other XML files that I have to use are many layers in depth.

谢谢

推荐答案

您在问题中描述的错误模式实际上是正确的行为,而不是错误.

What you describe as an error pattern in your question is actually correct behaviour and not an error.

如果您在 SimpleXMLElement 上使用 print_r(或 var_dump),它只会向您显示一些基本信息 关于该对象,不是 XML 文档的全部内容.

If you use print_r (or var_dump for that matter) on a SimpleXMLElement it will only show you some rudimentary information about that object and not the entire content of the XML document.

要查看整个 XML,请使用 asXML() 方法反而.或者,要获取调试信息,您可以使用专门了解如何调试 SimpleXMLElement 内容的库,例如 simplexml_debugIMSop 编写.

To see the whole XML, use the asXML() method instead. Alternatively to obtain debug information you can use a library that is specifically aware on how to debug SimpleXMLElement content's like simplexml_debug written by IMSop.

在您的特定情况下,对象显示为空(没有对象成员),因为默认命名空间内的 XML 文档中没有任何节点(请参阅:XML 1.0 中的命名空间) - 在 SimpleXMLElement 或多或少复杂的规则应用于数组转换a> - 会从对象内部调试处理程序传递给 print_rvar_dump 函数然后显示.所以只给出了类名,以便您可以看到它是某个类的对象 - 并且没有成员:

In your specific case, the object shows empty (no object members) because there aren't any nodes in the XML document within the default namespace (see: Namespaces in XML 1.0) that - after the more or less complex rules of SimpleXMLElement to array transitions were applied - would have been passed from the objects internal debug handler to the print_r or var_dump function to be displayed then. So only the class-name is given so that you can see it's an object of a certain class - and has no members:

SimpleXMLElement Object ( ) 
        ^          ^     ^ 
   object type     |     `------ object fields (empty)
                   |
             variable type

如何处理?很简单:了解 XML(您已经知道了)并从中获取记录的数据:

How to deal with that? It's very simple: Know the XML (you know it aleady) and obtain the data from it as it's documented:

  • PHP: Dealing with XML errors - Manual - Shows how you create (load) a SimpleXMLElement in a controlled manner and controlling error conditions.
  • PHP: Basic SimpleXML usage - Manual - Shows how to access data inside an XML document via the API of SimpleXMLElement.

由于您的 XML 元素名称包含冒号,您应该知道它们被称为XML 命名空间.正如您在 print_r 输出中看到的不同,您还将看到访问该命名空间内的子元素的不同.您必须使用 children() 方法 并指定其中的命名空间您想获取子元素.如果您使用 xpath() 方法访问元素,则需要先注册一个命名空间前缀,然后才能在调用该方法的 SimpleXMLElement 上使用它.

As your XML element names contain colons, you should be made aware that these are so called XML namespaces. As you've seen differences in the print_r output, you will also see differences in accessing the child elements within that namespace. You have to use the children() method and specify the namespace of which you would like to obtain the child elements. If you use the xpath() method to access elements, you need to register an namespace prefix before you use it on the SimpleXMLElement you call that method on.

网站上的一些SimpleXML 和 XML 命名空间相关问答材料:

  • Parse XML with Namespace using SimpleXML (Feb 2009)
  • PHP namespace simplexml problems (Jan 2010)
  • PHP SimpleXML Namespace Problem (May 2011)
  • Parse XML namespaces with php SimpleXML (May 2013)

您也可以从 PHP 手册中获取此信息,只需在 SimpleXMLElement API 文档.

You can also obtain this information from the PHP manual, just take a look for namespace related information within the SimpleXMLElement API documentation.

这篇关于php simplexml_load_file - 无法在print_r中看到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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