php - 此页面包含以下错误: creates html instead of XML

标签 php xml pdo

现在我收到此错误。

此页面包含以下错误: 第 1 列第 3 行出现错误:文档末尾有额外内容 下面是出现第一个错误之前的页面呈现。

不知何故,输出将我的 XML 文件呈现为 HTML。 我已经两次和三次检查了属性,它们是正确的...而且我似乎找不到错误...任何有线索的 friend 吗?

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

    class Areas{
    private $link; 
    public function __construct(){
    $this->link = new Connection();
    $this->link = $this->link->dbConnect();
}

  function fetch_area() {
    $query = $this->link->query("SELECT * FROM markers WHERE 1");  
    $query->setFetchMode(PDO::FETCH_ASSOC);

    header("Content-type: text/xml");

        while($row = $query->fetch()) {  
          $node = $dom->createElement("marker");  
          $newnode = $parnode->appendChild($node);   
          $newnode->setAttribute("name",$row['name']);
          $newnode->setAttribute("adress", $row['adress']);  
          $newnode->setAttribute("lat", $row['lat']);  
          $newnode->setAttribute("lng", $row['lng']);  
          $newnode->setAttribute("type", $row['type']);
        } 
   }
  }
$area = new Areas();
$area_info = $area->fetch_area(); 
echo $dom->saveXML();

最佳答案

为了快速修复,您应该在 fetch_area() 函数的开头添加以下代码。

global $dom, $node, $parnode;

这样你的函数就会像这样:

function fetch_area() {
    global $dom, $node, $parnode;
    $query = $this->link->query("SELECT * FROM markers WHERE 1");  
    $query->setFetchMode(PDO::FETCH_ASSOC);

    header("Content-type: text/xml");

        while($row = $query->fetch()) {  
          $node = $dom->createElement("marker");  
          $newnode = $parnode->appendChild($node);   
          $newnode->setAttribute("name",$row['name']);
          $newnode->setAttribute("adress", $row['adress']);  
          $newnode->setAttribute("lat", $row['lat']);  
          $newnode->setAttribute("lng", $row['lng']);  
          $newnode->setAttribute("type", $row['type']);
        } 
   }

另外为了更好的编码,你最好拿出来

header("Content-type: text/xml");

就在 echo $dom->saveXML(); 之前是很好的位置。

关于php - 此页面包含以下错误: creates html instead of XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16616739/

相关文章:

php - 使用自定义属性更新 Eloquent 模型

创建 fragment 时出现 java.lang.StackOverflowError

php - PDO,注入(inject)和节省服务器资源

php - 如何在PDO中将多个查询的结果添加到数组中?

java - PHP:SoapClient 构造函数非常慢(需要 3 分钟)

php - 在mysql中选择中间元素?

php - PayPal自适应支付IPN函数调用

mysql - 如何从 mysql 中的 xml 列中读取值?

java - 具有动态数组列表的 AutoCompletetextview

php - 42000 执行准备语句时查询语法错误