使用物理路径在PHP中加载XML文件


Loading XML file in PHP using physical path

当我使用文件的物理路径时,我在尝试使用PHP加载XML文件时遇到错误。当XML文件与代码文件位于同一目录中时,我不会遇到问题。下方给出了代码

<?php
$xml=simplexml_load_file(realpath('D:'XML'req.xml')) or die("Error: Cannot create object");
print_r($xml);
?>

任何帮助都将不胜感激。

谨致问候,Rahul

我不能提供关于simplxml的建议,但以下使用DOMDocument的方法确实有效。

<?php
    $path=realpath('C:'wwwroot'files'xml'bbc_rss.xml');
    if( $path ){
        libxml_use_internal_errors( true );
        $dom=new DOMDocument;
        $dom->standalone=true;
        $dom->validateOnParse=false;
        $dom->strictErrorChecking=false;
        $dom->recover=true;
        $dom->formatOutput=false;
        $dom->load( $path );
        libxml_clear_errors();
        echo '<pre>', print_r( $dom->saveXML() ,true ),'</pre>';
        $dom=null;
    } else {
        echo 'unable to find the xml file.';    
    }
?>

当我尝试使用一个不好的xml(即:格式不好或由于某种原因被认为无效的xml)文件时,没有显示任何内容,但使用我知道可以的xml文件,这很好。如果您不想使用DOMDocument方法,那么我是否可以建议您尝试使用interwebs中的已知xml文件作为测试?