现在我收到此错误。
此页面包含以下错误: 第 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/