[英]How can I send XML from PHP
I'm trying to send XML in my code but it also includes unwanted stuff. 我正在尝试在代码中发送XML,但其中还包含不需要的内容。 Here is my code:
这是我的代码:
<!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>
So what I get is like: 所以我得到的像是:
<!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>
But what I need is only the XML bits, like: 但是我只需要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>
If I use the following line of code 如果我使用以下代码
header('Content-type: text/xml');
I get this error: 我收到此错误:
This page contains the following errors: error on line 4 at column 14:
此页面包含以下错误:第4行第14列的错误:
XML declaration allowed only at the start of the document
仅在文档开始处允许XML声明
Below is a rendering of the page up to the first error.
下面是直到第一个错误的页面呈现。
What am I missing? 我想念什么? Thanks for the help.
谢谢您的帮助。
Multiple issues: 多个问题:
header
function at the start of your PHP script. header
函数放在PHP脚本的开头。 Use the following: 使用以下内容:
<?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);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.