【问题标题】:What is the reason of such error " XML or text declaration not at start of entity" when parsing xml?解析xml时出现这种错误“XML或文本声明不在实体开头”的原因是什么?
【发布时间】:2011-09-12 17:54:42
【问题描述】:

我通过调用页面getRout.php得到这个xml结果

<?xml version="1.0" encoding="UTF-8"?> 
<kml xmlns="http://earth.google.com/kml/2.0"> 
  <Document> 
    <name>KML Samples</name> 
    <open>1</open> 
    <distance>20.831418618295</distance> 
    <description>To enable simple instructions add: 'instructions=1' as parameter to the URL</description> 
    <Folder> 
.
.
.
.
</kml>

我使用这个 jquery 脚本检索 xml 响应:

$(document).ready(function(){
            $.ajax({
                type: "GET",
                url: "getRout.php",
                dataType: "xml",
                success: function(xml) {
                    $(xml).find('coordinates').each(function(){
                            var t = $(this).text();
                            alert(t);

                    });
                }
            });
        });

编辑

这是getRout.php文件的内容..

<?
require_once "RESTclient.class.php";
$url = "http://www.yournavigation.org/api/1.0/gosmore.php?format=kml&flat=52.215676&flon=5.963946&tlat=52.2573&tlon=6.1799&v=motorcar&fast=1&layer=mapnik";
header("Content-type:text/xml");
$result = RestClient::get($url,$inputs); 
echo($result->getResponse());
?>

【问题讨论】:

  • 通常这个问题意味着xml不以&lt;?xml?&gt;properly开头。检查之前是否有空格
  • 再次检查确实确保
  • 我已经编辑了问题..检查 getRout.php 脚本
  • 您使用的是 Firebug 还是类似的?该工具告诉您 XHR 响应的 exact 内容是什么?您是否尝试过直接使用浏览器访问getRout.php 页面?视图源说什么?
  • 直接访问 getRout.php .. 给我: XML Parsing Error: XML or text declaration not at start of entity Location: 127.0.0.1/direction2/getRout.php Line Number 2, Column 1: ^

标签: javascript jquery xml httpconnection


【解决方案1】:

看看getRout.php。关闭 ?&gt; 标记后是否有空格?您可以尝试完全删除结束标记,以确保您的响应中没有添加额外的空格。

https://softwareengineering.stackexchange.com/questions/89553/closing-tag-on-php-files

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 2011-11-06
    • 2011-12-30
    • 2013-11-13
    相关资源
    最近更新 更多