我试图在我的代码中发送XML,但它也包含不需要的东西。这是我的代码:
<!DOCTYPE html>
<html>
<body>
<?php
class InfoDB extends SQLite3{
function __construct(){
$this->open('info.db');
}
}
$db = new InfoDB();
$result = $db->query('SELECT * FROM members WHERE age=' . $q . '');
$xml = new DOMDocument();
$xml_info = $xml->createElement("Info");
$result = $db->query('SELECT * FROM members');
while($row = $result->fetchArray()) {
$xml_member = $xml->createElement("Member");
$name_attribute = $xml->createAttribute('Name');
$name_attribute->value = $row['name'];
$xml_member->appendChild($name_attribute);
$age_attribute = $xml->createAttribute('Age');
$age_attribute->value = $row['age'];
$xml_member->appendChild($age_attribute);
$xml_info->appendChild($xml_member);
}
$xml->appendChild( $xml_info );
$xml->save("members.xml");
$db->close();
print $xml->saveXML();
//// I tried the following lines with no luck
// $file = file_get_contents('club-members.xml');
// echo htmlspecialchars($file, ENT_QUOTES);
//// The following line means no output at all!!!
// header('Content-type: text/xml');
?>
</body>
</html>
所以我得到的是:
<!DOCTYPE html>
<html>
<body>
<?xml version="1.0"?>
<Info>
<Member Name="Person A" Age="23"/>
<Member Name="Person B" Age="33"/>
<Member Name="Person C" Age="43"/>
</Info>
</body>
</html>
但我需要的只是XML位,例如:
<?xml version="1.0"?>
<Info>
<Member Name="Person A" Age="23"/>
<Member Name="Person B" Age="33"/>
<Member Name="Person C" Age="43"/>
</Info>
如果我使用以下代码行
header('Content-type: text/xml');
我收到此错误:
此页面包含以下错误:第14行第4行的错误:
仅在文档开头提供XML声明
下面是第一个错误之前的页面呈现。
我错过了什么? 谢谢你的帮助。
答案 0 :(得分:0)
多个问题:
header
函数放在PHP脚本的开头。使用以下内容:
<?php
header('Content-type: text/xml');
class InfoDB extends SQLite3{
function __construct(){
$this->open('info.db');
}
}
$db = new InfoDB();
$result = $db->query('SELECT * FROM members WHERE age=' . $q . '');
$xml = new DOMDocument();
$xml_info = $xml->createElement("Info");
$result = $db->query('SELECT * FROM members');
while($row = $result->fetchArray()) {
$xml_member = $xml->createElement("Member");
$name_attribute = $xml->createAttribute('Name');
$name_attribute->value = $row['name'];
$xml_member->appendChild($name_attribute);
$age_attribute = $xml->createAttribute('Age');
$age_attribute->value = $row['age'];
$xml_member->appendChild($age_attribute);
$xml_info->appendChild($xml_member);
}
$xml->appendChild( $xml_info );
$xml->save("members.xml");
$db->close();
print $xml->saveXML();
//// I tried the following lines with no luck
// $file = file_get_contents('club-members.xml');
// echo htmlspecialchars($file, ENT_QUOTES);
?>