【问题标题】:simplexml_import_dom - does not worksimplexml_import_dom - 不起作用
【发布时间】:2009-08-25 08:22:18
【问题描述】:
if (file_exists('http://something_from_a_url.xml')) { $xml = simplexml_import_dom('http://something_from_a_url.xml'); print_r($xml); } 别的 { exit('打开xml文件失败'); }

当我执行这段代码时,它说打开失败,我不明白为什么..

【问题讨论】:

  • 那么,您确定该文件确实存在吗?
  • 检查您的错误日志,可能是 url 包装器已被禁用,然后您无法加载这样的文件。
  • 是的,上述xml文件存在,我可以通过url而不是这段代码访问它。用你的任何一个替换我拥有的xml文件并执行。
  • 伙计们..你不会相信,但我只是颠倒了 if 循环中的语句,它起作用了,不是通过 simplexml,而是通过 file_get_contents。有人对此有什么解释吗?
  • 现在它可以通过 simplexml_file_upload 工作,但它是否可以反过来工作?

标签: php


【解决方案1】:

simplexml_import_dom 采用 DOM 节点,而不是 URL。你应该使用simplexml_load_file intead:

$xml = simplexml_load_file('http://something_from_a_url.xml');
print_r($xml);

【讨论】:

  • 重新评论您对该问题的最新评论,完全删除 file_exists if 语句会解决此问题。您“颠倒陈述”基本上只是意味着 simplexml_load_file 先运行,而在它之前永远不会运行。
【解决方案2】:

file_exists() 不适用于 URL。这对 URL 来说甚至没有意义。

simplexml_import_dom() 旨在将 DOM 节点转换为 SimpleXML 节点。它需要一个 DOM 节点的实例作为参数。您正在传递一个 URL。

也许你想:

$simplexml = simplexml_load_string(file_get_contents($url));

【讨论】:

  • 它的 simplexml 不起作用,不,那段代码不起作用
  • 在尝试上述代码之前,您是否取出了 file_exists 检查?如本线程其他地方所述,file_exists 不适用于 http(s)。
【解决方案3】:

检查:

a) simplexml_import_dom 的参数是一个 DOMNode。

b) 您在 php.ini 中启用了 allow_url_fopen

作为替代方案,您可以尝试将 [simplexml_load_file][3] 与 URL 一起使用,看看发生了什么。

额外链接: php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen

php.net/manual/en/function.simplexml-load-file.php

【讨论】:

    【解决方案4】:

    一般ini设置禁止这个。

    你可以试试这个方法来验证远程文件是否存在。

    if (fopen($url, "r")) {
        echo "File Exists";
    } else {
        echo "Can't Connect to File";
    }
    

    【讨论】:

    • 查看有问题的最新评论
    【解决方案5】:

    做什么

    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    $contents = file_get_contents('http://something_from_a_url.xml');
    echo '<div>Debug: len(contents)=', strlen($contents), "</div>\n";
    
    libxml_use_internal_errors(true);
    libxml_clear_errors();
    $xml = new SimpleXMLElement($contents);
    
    foreach( libxml_get_errors() as $error ) {
      echo 'error: ', $error->$message, "<br />\n";
    }
    

    打印?

    【讨论】:

      【解决方案6】:

      反之亦然,不知道为什么——但解决了

      【讨论】:

        猜你喜欢
        • 2023-04-09
        • 1970-01-01
        • 2019-01-07
        • 1970-01-01
        • 2016-03-13
        • 1970-01-01
        • 1970-01-01
        • 2016-09-12
        • 1970-01-01
        相关资源
        最近更新 更多