我在阅读XML时遇到了麻烦,我跟着this tutorial,并生成了以下代码。
当我打开控制器时,我得到了
解析错误:在第49行的C:\ wamp \ www \ ci_doctrine \ system \ application \ controllers \ welcome.php中解析错误,期待T_STRING' or
T_VARIABLE'或'T_NUM_STRING'
第49行是foreach($ xmlData ['item']为$ row)我不知道该怎么做,建议?帮助真的很感激
这是我的代码:
<?php
class Welcome extends Controller {
/*function Welcome()
{
parent::Controller();
}
*/
function index()
{
//$this->load->view('welcome_message');
//load the parser library
$this->load->library('parser');
$data['title'] = 'Parsing XML using Simplexml class of CodeIgniter';
$data['products'] = $this->_getXML('myxml');
$this->parser->parse('table_view', $data);
//$this->load->library('parser');
//$data['title'] = 'Parsing XML using SimpleXML class of CodeIgniter';
}
function _getXML($fname)
{
$filename = $fname.'.xml';
$xmlfile="“C:\wamp\www\" . $filename;
$xmlRaw = file_get_contents($xmlfile);
$this->load->library('simplexml');
$xmlData = $this->simplexml->xml_parse($xmlRaw);
foreach($xmlData['item'] as $row)
{
$result .= '<tr>';
$result .= '<td>'.$row['id'].'</td>';
$result .= '<td>'.$row['name'].'</td>';
$result .= '<td>'.$row['category'].'</td>';
$result .= '<td>$ '.$row['price'].'</td>';
$result .= '</tr>';
}
return $result;
}
}
//End of file welcome.php
// Location: ./system/application/controllers/welcome.php
?>
<item>
<id>1</id>
<name>iPhone</name>
<category>Mobile</category>
<price>300</price>
</item>
<item>
<id>2</id>
<name>iMac</name>
<category>Desktop Computers</category>
<price>2500</price>
</item>
<item>
<id>3</id>
<name>MacBook Pro</name>
<category>Mobile PC</category>
<price>2000</price>
</item>
<item>
<id>4</id>
<name>iTouch</name>
<category>Gadgets</category>
<price>150</price>
</item>
<item>
<id>5</id>
<name>Wii</name>
<category>Gaming</category>
<price>1250</price>
</item>
<item>
<id>6</id>
<name>Time Capsule</name>
<category>Acessories</category>
<price>1000</price>
</item>
<item>
<id>7</id>
<name>Apple TV</name>
<category>Gadgets</category>
<price>800</price>
</item>
答案 0 :(得分:2)
$xmlfile="“C:\wamp\www\"
答案 1 :(得分:1)
在第
行$xmlfile="“C:\wamp\www\" . $filename;
您正在使用必须转义的反斜杠。将其更改为
$xmlfile="C:\\wamp\\www\\" . $filename;