URL:XML解析错误:格式不正确

时间:2016-03-22 18:14:48

标签: php xml xml-parsing

我有这个错误:

XML parsing error : not well formed
Location : http://127.0.0.1/localwebsite/map-control-event-type-criteria.php?=&eP=chose&tP=pub_beer&aP[0]=eating&aT=washington
Number of line 1, Column 2 :

源代码似乎是正确的:我明白了:

    <?xml version="1.0"?>
    <markers><marker name="the name" address="the adress" lat="46.187424" lng="8.717437" id="1" town="washington"/></markers>

页面的php代码如下所示:

    <?php
    require 'inc/bootstrap.php';
    require_once 'inc/db.php';

    $db = app::getdatabase();

    $evtpar=$_GET['eP'];
    $typepar=$_GET['tP'];
    $critpar=$_GET['aP']; 
    $townURL=$_GET['aT'];

    // some php code to test the validity of URL parameters

    $dom = new DOMDocument("1.0");
    $node = $dom->createElement("markers");
    $parnode = $dom->appendChild($node); 


    $result=$db->query("SELECT name.....)->fetchall(); 

    header("Content-type: text/xml");
    $i=0;
    foreach ($result as $row)
    {
       $node = $dom->createElement("marker");
       $newnode = $parnode->appendChild($node);
       $newnode->setAttribute("name", $row->name);
       $newnode->setAttribute("address", $row->address);
       $newnode->setAttribute("lat", $row->lat);
       $newnode->setAttribute("lng", $row->lng);
       $newnode->setAttribute("id", $row->inf_id);
       $newnode->setAttribute("town", $row->town);

       $i++;
    }   

     echo $dom->saveXML();

我怎么知道问题的来源?

第1行是否意味着问题来自网址或以下代码?

编辑:上述代码前的文件:

在到达URL之前,我必须使用以前的代码:

    File1 php:
    $mapDisplay='EventCritType';

    File2 included in File1:

    <?php if($mapDisplay=='EventCritType'): 
    echo     $Map->mapeventcrittypeview($town,$eventparam,$typeparam,$activityparams,$lat,$lng,13); 
    endif; ?>

    file3 = Map Class:

    public function mapeventcrittypeview($town,$eventparam,$typeparam,$activityparams,$lat,$lng,$zoom)
    {

        $evtparam=http_build_query(array('eP'=>$eventparam));
        $tpparam=http_build_query(array('tP'=>$typeparam));
        $actparam=http_build_query(array('aP'=>$activityparams));
        $city=http_build_query(array('aT'=>$town));

        $url="/localwebsite/map-control-event-type-criteria.php?=&$evtparam&$tpparam&$actparam&$city";

        include('inc/map-script.php');

    }

    File4= map-script.php

    script type="text/javascript">

    var infoWindow = new google.maps.InfoWindow;

    function load() {
      var map = new google.maps.Map(
        document.getElementById("map"), {
          center: new google.maps.LatLng(<?= json_encode($lat); ?>, <?= json_encode($lng);?>),
          zoom: <?php echo json_encode($zoom); ?>,
          mapTypeId: 'roadmap'
        });

      downloadUrl(<?= json_encode($url); ?>, function(data) {
      var xml = data.responseXML;
      var markers = xml.documentElement.getElementsByTagName("marker");
      for (var i = 0; i < markers.length; i++) {
        var name = markers[i].getAttribute("name");
        var address = markers[i].getAttribute("address");
        var id = markers[i].getAttribute("id");
        var town = markers[i].getAttribute("town").toLowerCase();
        var point = new google.maps.LatLng(
        parseFloat(markers[i].getAttribute("lat")),
        parseFloat(markers[i].getAttribute("lng")));
        var html = "<b>" + name + "</b> <br/>" + address;
        createMarker(point, id, town, html, i, map);

      }
      });
    }

    ....

    </script>

    File5= map-control-event-type-criteria.php

    <?php
    require 'inc/bootstrap.php';
    require_once 'inc/db.php';

    $db = app::getdatabase();
    $evtpar=$_GET['eP'];
    $critpar=$_GET['aP']; 
    $townURL=$_GET['aT'];

    ....

2 个答案:

答案 0 :(得分:1)

  

第1行的数量是否意味着问题来自网址或以下代码?

表示该网址返回的数据。

当我使用XMLlint测试代码时,它说:

  

test.xml:1:解析器错误:仅在文档开头允许XML声明

XML声明必须是文档中非常的第一件事。

你之前有空格,这是不允许的。

答案 1 :(得分:0)

尝试使用标题:

<?xml version="1.0" encoding="UTF-8"?>

如果没有解决问题,你可以查看代码,如果你把代码我可以帮助你。