无法使用 simplexml_load_file() 访问 XML 文档 [英] Cannot access XML Document using simplexml_load_file()

查看:31
本文介绍了无法使用 simplexml_load_file() 访问 XML 文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助!我正在尝试从托管提供商的网站数据库中生成 XML 文档,并从我的家用 PC 访问它,现在我正在本地主机环境中执行此操作.我使用这些代码并在浏览器中收到错误消息.请看看我的代码...

I NEED HELP! I'm trying to generate XML document from my website database in hosting provider and accessing it from my home PC, right now I'm doing it in localhost environment. I use these codes and I got error message in browser. Please take a look at my codes...

data.php 用于生成 XML 文档

data.php for generating XML document

<?php
include ("koneksi-database.php");

header('Content-Type: text/xml');
echo "<?xml version='1.0'?>";
echo "<outbox>";

$query = "SELECT * FROM `tbstatus` WHERE `status` = '$status' ORDER BY `ID`";
$result = mysql_query($query);

while ($data = mysql_fetch_array($result))
{
echo "<data>";


    echo "<trx>".$data['trx']."</trx>";
    echo "<status>".$data['status']."</status>";
    echo "<signature>".$data['signature']."</signature>";
    echo "<time>".$data['time']."</time>";


echo "</data>";
}

echo "</outbox>";
?>

read.php 用于读取 localhost 中的 XML 文档

read.php for reading the XML document in localhost

print_r($dataxml = simplexml_load_file('data.php'));

我还创建了 index.php 以每 5 秒运行一次脚本.

I also create index.php for running the script every 5 seconds.

<html>
<head>
<script type="text/javascript">

var xmlhttp;

function ajax()
{

    if (window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest();
        }
    else
    {
        xmlhttp =new ActiveXObject("Microsoft.XMLHTTP");
        }

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            // do nothing
            }
        }


    xmlhttp.open("GET","read.php");
    xmlhttp.send();

    setTimeout("ajax()", 5000);
    }


</script>
</head>
<body onload="ajax();">
    <h1>Server is Running...</h1>
</body>
</html>

最后这是我打开 read.php 时从浏览器收到的错误消息.

finally this the error messages that i get from browser when I open read.php.

Warning: simplexml_load_file() [function.simplexml-load-file]: data.php:9: parser error : Start tag expected, '<' not found in C:\xampplite\htdocs\simpul\baca.php on line 9

Warning: simplexml_load_file() [function.simplexml-load-file]: echo "<?xml version='1.0'?>"; in C:\xampplite\htdocs\simpul\baca.php on line 9

Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in C:\xampplite\htdocs\simpul\baca.php on line 9

推荐答案

您的 print_r($dataxml = simplexml_load_file('data.php')); 正在读取原始 PHP 文件,而不是脚本执行结果!

Your print_r($dataxml = simplexml_load_file('data.php')); is reading you raw PHP file, not the script execution result!

data.php 文件包含输出 XML 文件的 PHP 代码,而不是真正的 XML 文件.

data.php file have a PHP code that outputs a XML file, not a really XML file.

你应该使用 print_r($dataxml = simplexml_load_file('http://localhost/data.php')); 为例.(假设 http://localhost/data.php 是访问文件的 url.)

You should use print_r($dataxml = simplexml_load_file('http://localhost/data.php')); for example. (Assuming that http://localhost/data.php is the url to access your file.)

仅使用 'data.php' 作为参数将从服务器获取原始文件,而不是由 PHP 处理.

using only 'data.php' as parameter will get the raw file from server, not processed by PHP.

这篇关于无法使用 simplexml_load_file() 访问 XML 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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