使用 PHP 在 HTML table 中渲染 XML 输出
Render XML output in HTML table using PHP
我有 XML 输出写在 PHP 中,如下所示
<Twitch_XML>
<twitch>
<Bird id="1">
<Species>Willow ptarmigan</Species>
<Age>2</Age>
<Sex>Male</Sex>
<Location>London</Location>
<Image>
http://www.kidzone.ws/images-changed/birds/willow_ptarmigan.jpg
</Image>
<User>Chamith</User>
</Bird>
<Bird id="5">
<Species>Grey partridge</Species>
<Age>2</Age>
<Sex>Male</Sex>
<Location>London</Location>
<Image>
https://www.rspb.org.uk/Images/greypartridge_tcm9-17615.jpg?width=530&crop=(196,364,1006,820)
</Image>
<User>Raveen</User>
</Bird>
</twitch>
</Twitch_XML>
我想将此输出放在 HTML table 中。请注意,我没有使用外部 XML 文档。我从 MySQL 检索数据并使用 savexml()
方法
直接打印到 XML
要在 HTML table 中呈现数据,我尝试使用以下代码:
$twitcdm->saveXML();
foreach($twitcdm->Twitch_XML->twitch as $xmv){
echo "<tr>";
echo "<td>{$xmv->id}</td>";
echo "<td>{$xmv->Species}</td>";
echo "<td>{$xmv->Age}</td>";
echo "<td>{$xmv->Sex}</td>";
echo "<td>{$xmv->Location}</td>";
echo "<td>{$xmv->Image}</td>";
echo "<td>{$xmv->User}</td>";
echo "<br />";
echo "</tr>";
}
但是当我 运行 这给了我一个错误:
Warning: Invalid argument supplied for foreach() in
C:\Winginx\home\localhost\public_html\chamith\twitch_id.php on line 67
这是我用来生成 XML 输出的全部 PHP 代码
$id = $_GET["twitch_id"];
$command = "SELECT id,species,age,sex,location,image,username FROM chamith_twitch.twitch WHERE id = $id";
$dboutput = $mysqlconnection->query($command);
$mysqlconnection->close();
$no_of_raws = $dboutput->num_rows;
if ($no_of_raws > 0) {
while ($line = $dboutput->fetch_array()) {
$lines[] = $line;
}
$dboutput->close();
$twitcdm = new DOMDocument();
// $twitcdm->formatOutput = true;
$twitcdm->appendChild($twitcdm->createElement('Twitch_XML'));
$fetch_xml = $twitcdm->documentElement;
$xmldoc = $twitcdm->createElement('twitch');
foreach ($lines as $line) {
$dbpkey = $line['id'];
$twitch = $twitcdm->createElement('Bird');
$twitch->setAttribute("id", $line['id']);
$Species = $twitcdm->createElement('Species');
$age = $twitcdm->createElement('Age');
$Sex = $twitcdm->createElement('Sex');
$address = $twitcdm->createElement('Location');
$twitch_photo = $twitcdm->createElement('Image');
$record_owner = $twitcdm->createElement('User');
$header = $twitcdm->createTextNode($line['species']);
$Species->appendChild($header);
$twitch->appendChild($Species);
$header = $twitcdm->createTextNode($line['age']);
$age->appendChild($header);
$twitch->appendChild($age);
$header = $twitcdm->createTextNode($line['sex']);
$Sex->appendChild($header);
$twitch->appendChild($Sex);
$header = $twitcdm->createTextNode($line['location']);
$address->appendChild($header);
$twitch->appendChild($address);
$header = $twitcdm->createTextNode($line['image']);
$twitch_photo->appendChild($header);
$twitch->appendChild($twitch_photo);
$header = $twitcdm->createTextNode($line['username']);
$record_owner->appendChild($header);
$twitch->appendChild($record_owner);
$xmldoc->appendChild($twitch);
}
$fetch_xml->appendChild($xmldoc);
// header('Content-type: text/xml');
$twitcdm->saveXML();
我想在 table 中填充这些 XML 数据,我该如何解决这个问题?
这里有几个问题:
1.无效 XML
查看 http://www.xmlvalidation.com 并测试您的 XML。原来这一行
https://www.rspb.org.uk/Images/greypartridge_tcm9-17615.jpg?width=530&crop=(196,364,1006,820)
导致问题,因为它包含 无效字符。请参阅 Invalid Characters in XML on this site. You may want to check out How do you make strings XML"safe"? 了解解决方案。
2。 XML 对象的错误引用
foreach ($twitcdm->Twitch_XML->twitch as $xmv) {
// etc.
}
$twitcdm
表示根节点<Twitch_XML>
- 你忘记了上面XML路径中的
<Bird>
节点
改为:
foreach ($twitcdm->twitch->Bird as $xmv) {
echo $xmv->Image . PHP_EOL;
// etc.
}
查看使用 CDATA
在 XML 中包含无效字符并在 foreach
中包含正确 XML 路径的工作示例:https://eval.in/321334
(使用 SimpleXML 而不是 DOM,不过没什么大不了的)
备注:
由于您自己创建了 XML,因此您可以在其中包含对无效字符的处理。此外,我建议在命名节点时使用一致的 uppercase/lowercase,例如<twitch>
但 <Bird>
.
我有 XML 输出写在 PHP 中,如下所示
<Twitch_XML>
<twitch>
<Bird id="1">
<Species>Willow ptarmigan</Species>
<Age>2</Age>
<Sex>Male</Sex>
<Location>London</Location>
<Image>
http://www.kidzone.ws/images-changed/birds/willow_ptarmigan.jpg
</Image>
<User>Chamith</User>
</Bird>
<Bird id="5">
<Species>Grey partridge</Species>
<Age>2</Age>
<Sex>Male</Sex>
<Location>London</Location>
<Image>
https://www.rspb.org.uk/Images/greypartridge_tcm9-17615.jpg?width=530&crop=(196,364,1006,820)
</Image>
<User>Raveen</User>
</Bird>
</twitch>
</Twitch_XML>
我想将此输出放在 HTML table 中。请注意,我没有使用外部 XML 文档。我从 MySQL 检索数据并使用 savexml()
方法
要在 HTML table 中呈现数据,我尝试使用以下代码:
$twitcdm->saveXML();
foreach($twitcdm->Twitch_XML->twitch as $xmv){
echo "<tr>";
echo "<td>{$xmv->id}</td>";
echo "<td>{$xmv->Species}</td>";
echo "<td>{$xmv->Age}</td>";
echo "<td>{$xmv->Sex}</td>";
echo "<td>{$xmv->Location}</td>";
echo "<td>{$xmv->Image}</td>";
echo "<td>{$xmv->User}</td>";
echo "<br />";
echo "</tr>";
}
但是当我 运行 这给了我一个错误:
Warning: Invalid argument supplied for foreach() in
C:\Winginx\home\localhost\public_html\chamith\twitch_id.php on line 67
这是我用来生成 XML 输出的全部 PHP 代码
$id = $_GET["twitch_id"];
$command = "SELECT id,species,age,sex,location,image,username FROM chamith_twitch.twitch WHERE id = $id";
$dboutput = $mysqlconnection->query($command);
$mysqlconnection->close();
$no_of_raws = $dboutput->num_rows;
if ($no_of_raws > 0) {
while ($line = $dboutput->fetch_array()) {
$lines[] = $line;
}
$dboutput->close();
$twitcdm = new DOMDocument();
// $twitcdm->formatOutput = true;
$twitcdm->appendChild($twitcdm->createElement('Twitch_XML'));
$fetch_xml = $twitcdm->documentElement;
$xmldoc = $twitcdm->createElement('twitch');
foreach ($lines as $line) {
$dbpkey = $line['id'];
$twitch = $twitcdm->createElement('Bird');
$twitch->setAttribute("id", $line['id']);
$Species = $twitcdm->createElement('Species');
$age = $twitcdm->createElement('Age');
$Sex = $twitcdm->createElement('Sex');
$address = $twitcdm->createElement('Location');
$twitch_photo = $twitcdm->createElement('Image');
$record_owner = $twitcdm->createElement('User');
$header = $twitcdm->createTextNode($line['species']);
$Species->appendChild($header);
$twitch->appendChild($Species);
$header = $twitcdm->createTextNode($line['age']);
$age->appendChild($header);
$twitch->appendChild($age);
$header = $twitcdm->createTextNode($line['sex']);
$Sex->appendChild($header);
$twitch->appendChild($Sex);
$header = $twitcdm->createTextNode($line['location']);
$address->appendChild($header);
$twitch->appendChild($address);
$header = $twitcdm->createTextNode($line['image']);
$twitch_photo->appendChild($header);
$twitch->appendChild($twitch_photo);
$header = $twitcdm->createTextNode($line['username']);
$record_owner->appendChild($header);
$twitch->appendChild($record_owner);
$xmldoc->appendChild($twitch);
}
$fetch_xml->appendChild($xmldoc);
// header('Content-type: text/xml');
$twitcdm->saveXML();
我想在 table 中填充这些 XML 数据,我该如何解决这个问题?
这里有几个问题:
1.无效 XML
查看 http://www.xmlvalidation.com 并测试您的 XML。原来这一行
https://www.rspb.org.uk/Images/greypartridge_tcm9-17615.jpg?width=530&crop=(196,364,1006,820)
导致问题,因为它包含 无效字符。请参阅 Invalid Characters in XML on this site. You may want to check out How do you make strings XML"safe"? 了解解决方案。
2。 XML 对象的错误引用
foreach ($twitcdm->Twitch_XML->twitch as $xmv) {
// etc.
}
$twitcdm
表示根节点<Twitch_XML>
- 你忘记了上面XML路径中的
<Bird>
节点
改为:
foreach ($twitcdm->twitch->Bird as $xmv) {
echo $xmv->Image . PHP_EOL;
// etc.
}
查看使用 CDATA
在 XML 中包含无效字符并在 foreach
中包含正确 XML 路径的工作示例:https://eval.in/321334
(使用 SimpleXML 而不是 DOM,不过没什么大不了的)
备注:
由于您自己创建了 XML,因此您可以在其中包含对无效字符的处理。此外,我建议在命名节点时使用一致的 uppercase/lowercase,例如<twitch>
但 <Bird>
.