我正在嘗試使用codeigniter & php將我的xml文件中的數據保存到mysql數據庫中。但我找不到一種方法來轉義特殊的xml字符,以將我的數據從xml文件保存到我的表cache_table。在Codeigniter/php中轉義xml字符
Code:
$xml_source = str_replace(array("&", "&"), array("&", "&"),file_get_contents('XML/customer.xml'));
$xml = simplexml_load_string($xml_source);
foreach($xml as $row)
{
$attr = $row->attributes();
$results[] = array('CustomerAccountNumber' => $this->db->escape_str($attr->CustomerAccountNumber),'Desciption' => $this->db->escape_str($attr->Desciption)):
$this->db->insert_batch('cache_cust',$results);
但我收到此錯誤:
錯誤:
simplexml_load_string(): Entity: line 37: parser error : Unescaped '<' not allowed in attributes values.
customer.xml
<?xml version="1.0" standalone="yes"?>
<Rows>
<Row CustomerAccountNumber="12" Name="Arj" Desciption="PJ psm" />
<Row CustomerAccountNumber="1" Name="Aj" Desciption="*/Artic" />
<Row CustomerAccountNumber="2" Name="smla" Desciption="& light" />
<Row CustomerAccountNumber="3" Name="alik" Desciption="*/Artic" />
<Row CustomerAccountNumber="4" Name="wqla" Desciption="" />
<Row CustomerAccountNumber="5" Name="walij" Desciption="16-17" />
<Row CustomerAccountNumber="6" Name="li" Desciption="[email protected]" />
</Rows>
我怎樣才能避免這種錯誤,避免特殊xml字符如此我的代碼保存了xml文件中的所有記錄到數據庫中?
可以粘貼您的XML數據? – GBD
@GBD我在question.thanks中添加了xml數據。 –
在XML中具有特殊含義的字符在SQL中不可能有特殊含義。你爲什麼認爲你需要逃避任何事情? –