【问题标题】:How to display raw xml file in php to web browser [duplicate]如何将php中的原始xml文件显示到Web浏览器[重复]
【发布时间】:2020-01-15 14:14:37
【问题描述】:

您好,我有一个 xml 文件,我想在网站上显示它。 我尝试了所有我发现并尝试自己做的事情,但我是任何代码语言的新手。 我现在的代码是这样的:

<?php
header('Content-type: text/xml');

    $xml = simplexml_load_file(xmlfile.xml) ;

echo $xml ;



?>

当我访问我的网站时,我看到的输出只是一个警告:此 XML 文件似乎没有任何与之关联的样式信息。文档树如下所示。

但我什么都看不到。那么请有人帮我写一个输出整个 xml 文件的代码,包括 xml 声明、标签和节点值?

【问题讨论】:

  • 与其使用$xml = simplexml_load_file(xmlfile.xml) ; 进行转换,不如使用readfile("xmlfile.xml"); 显示文件

标签: php xml file browser new-operator


【解决方案1】:

试试这个代码。它对我有用。

<?php
  header("Content-type: text/xml");
  $yourFile = "xmlfile.xml";
  $file = file_get_contents($yourFile);

  echo $file;

如果你坚持简单的xml你可以这样写。

$xml = simplexml_load_file("xmlfile.xml");

echo $xml->asXML();

【讨论】:

    【解决方案2】:

    你不必使用simplexml_load_file:还有另一个函数可以读取文件,这个函数是file_get_contents($filename)
    这是一个简单的代码:

    <?php
    // Set the encoding to XML
    header('Content-type: text/xml');
    
    // Get contents of the file
    $xml = file_get_contents("xmlfile.xml") ;
    
    // Print contents
    echo $xml;
    
    ?>
    

    希望对你有帮助! 对于语言错误,我们深表歉意;)

    【讨论】:

    • 此答案与上一个答案相同。它没有添加任何新内容。
    猜你喜欢
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    相关资源
    最近更新 更多