Php 文件\u获取\u内容XML foreach循环不工作
此文件应输出一系列串联字符串,如下所示:Php 文件\u获取\u内容XML foreach循环不工作,php,xml,simplexml,Php,Xml,Simplexml,此文件应输出一系列串联字符串,如下所示: "id number" "name of the fruit" "color of the fruit" 但是,当我测试这个时,它只会给我一个空白屏幕,除非我使用var_dump来回显信息。这些信息来自一个数据库,并汇编在一个名为fruitxml.php的文件中。这就是我的$contents变量的来源文件 甚至echo$xml也不起任何作用。这至少应该行得通。但事实并非如此。我做错了什么,给了我一个空白屏幕 <?php $cont
"id number" "name of the fruit" "color of the fruit"
但是,当我测试这个时,它只会给我一个空白屏幕,除非我使用var_dump来回显信息。这些信息来自一个数据库,并汇编在一个名为fruitxml.php的文件中。这就是我的$contents变量的来源文件
甚至echo$xml也不起任何作用。这至少应该行得通。但事实并非如此。我做错了什么,给了我一个空白屏幕
<?php
$contents = file_get_contents("http://localhost:8888/SSL/Week3/Activity%203-3/fruitxml.php");
$xml = new SimpleXMLElement($contents);
echo $xml;
foreach($xml->fruits as $fruit) {
echo $fruit->fruitid . $fruit->fruitcolor . $fruit->fruitname . '.<br />';
} ?>
<pre><?php var_dump($xml) ?></pre>
这是一个包含XML的文件,它从数据库中获取信息
<?php
$curl = curl_init('http://localhost/SSL/Week3/Activity%203-3/fruitxml.php');
curl_setopt($curl, CURLOPT_PORT, 8888);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 8888);
$contents = curl_exec($curl);
$xml = new SimpleXMLElement($contents);
echo $xml;
foreach($xml->fruits as $fruit) {
echo $fruit->fruitid . $fruit->fruitcolor . $fruit->fruitname . '.<br />';
} ?>
<pre><?php var_dump($xml) ?></pre>
这主要是因为您使用的是file_get_内容,url看起来可疑,因为使用了8888端口 考虑改用CURL,它应该可以工作:
function escapeXml($input)
{
return htmlspecialchars($input, ENT_XML1, 'UTF-8');
}
$xmlfile = '<?xml version="1.0" encoding="UTF-8"?>';
$xmlfile .= "<fruits>";
foreach($result as $user) {
$xmlfile .= '<fruit>';
$xmlfile .= "<id>" . escapeXml($user['fruitid']) . "</id>";
$xmlfile .= "<name>" . escapeXml($user['fruitname']) . "</name>";
$xmlfile .= "<color>" . escapeXml($user['fruitcolor']) . "</color>";
$xmlfile .= '</fruit>';
};
$xmlfile .= "</fruits>";
生成如下所示的xml: 输出如下: 说明: 文档的根节点由SimpleXmlElement本身表示。因此,您不必使用$xml->fruits$xml已经表示水果节点 要访问单个水果,请将它们分别放置在一个节点中 没有必要将水果的属性放在两个嵌套节点中。对每个属性使用单个节点 使用htmlspecialchars转义特殊字符,以便在例如水果名称将包含<或&时不会出现语法错误
//代码未测试空白屏幕表示有东西坏了。将这些行添加到“显示错误”集合中,1;错误报告全部;在PHP脚本的最顶端,查看是否产生任何错误。请尝试。没有错误。只是一个空白屏幕。请向我们展示var_dump$xmlI的输出,它刚刚添加到问题中^xml文档的结构不适合您想要解释它的方式。xml的结构是固定的还是可以更改?是的,我的apache位于8888端口。我的MySQL在8889上。这以前从来都不是问题。我也用了你的方式,它仍然是一个空白屏幕。谢谢!你是第一个明白这一点的人,并且在我的浏览器中显示了一些东西。谁知道我所要做的就是简化我的XML。注意:与其将XML构建为字符串,不如像这里所解释的那样使用库来实现这一点
<?php
$curl = curl_init('http://localhost/SSL/Week3/Activity%203-3/fruitxml.php');
curl_setopt($curl, CURLOPT_PORT, 8888);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 8888);
$contents = curl_exec($curl);
$xml = new SimpleXMLElement($contents);
echo $xml;
foreach($xml->fruits as $fruit) {
echo $fruit->fruitid . $fruit->fruitcolor . $fruit->fruitname . '.<br />';
} ?>
<pre><?php var_dump($xml) ?></pre>
function escapeXml($input)
{
return htmlspecialchars($input, ENT_XML1, 'UTF-8');
}
$xmlfile = '<?xml version="1.0" encoding="UTF-8"?>';
$xmlfile .= "<fruits>";
foreach($result as $user) {
$xmlfile .= '<fruit>';
$xmlfile .= "<id>" . escapeXml($user['fruitid']) . "</id>";
$xmlfile .= "<name>" . escapeXml($user['fruitname']) . "</name>";
$xmlfile .= "<color>" . escapeXml($user['fruitcolor']) . "</color>";
$xmlfile .= '</fruit>';
};
$xmlfile .= "</fruits>";
foreach($xml->fruit as $fruit) {
echo $fruit->id . $fruit->color . $fruit->name . '.<br />';
}