[請益] php生成xml 中文字無法顯示問題
各位大家好
想請問各位,目前問題是想取得mssql內的資料,生成xml在網頁上,
但是只要是資料表欄位資料輸入為中文,生成xml就會出現錯誤,
錯誤訊息為b>Warning</b>: DOMElement::setAttribute() [<a
href='domelement.setattribute'>domelement.setattribute<...
附上原碼:
<?php
ob_start();
header('Content-Type: text/xml');
$dom = new DOMDocument('1.0');
$dom->encoding = 'UTF-8';
$conn = mssql_connect("localhost", "sa", "12345");
mssql_select_db("DaShang", $conn);
$SQL="SELECT * FROM test"; //資料庫存在,建立SQL命令字串
$datalist=mssql_query($SQL);//將回傳結果存放於變數中
$root = $dom->createElement('root'); $dom->appendChild($root);// 建立母節點
$root
while ($fielddatas=mssql_fetch_array($datalist)) //將資料錄轉換為欄位陣列集合
{
$child1 = $dom->createElement('item'); $root->appendChild($child1);
//輸出欄位資料
$child1->setAttribute('name', $fielddatas["name"]); // 設定屬性
$child1->setAttribute('phone', $fielddatas["phone"]);
$child1->setAttribute('qnum', $fielddatas["qnum"]); // 設定屬性
//$child1->setAttribute('question', $fielddatas["question"]);
}
$xmlStr = $dom->saveXML(); echo $xmlStr;
?>
其中這行//$child1->setAttribute('question', $fielddatas["question"]);
只要槓掉就可以完整生成xml,
<?xml version="1.0" encoding="UTF-8" ?>
- <root>
<item name="jack" phone="0933" qnum="2" />
<item name="jame/s" phone="0912" qnum="2" />
<item name="mary" phone="0920" qnum="3" />
</root>
想請教各位//$child1->setAttribute('question', $fielddatas["question"]);
這行是否要改用別的寫法去存取?或是有其他地方沒注意到的編碼要轉換的問題?
再拜託各位了~感謝!!!!!!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.44.3.165
→
02/16 13:48, , 1F
02/16 13:48, 1F
→
02/16 13:59, , 2F
02/16 13:59, 2F
推
02/16 15:11, , 3F
02/16 15:11, 3F
→
02/16 15:14, , 4F
02/16 15:14, 4F
→
02/16 16:11, , 5F
02/16 16:11, 5F
→
02/16 16:23, , 6F
02/16 16:23, 6F
→
02/16 16:52, , 7F
02/16 16:52, 7F
推
02/16 17:09, , 8F
02/16 17:09, 8F
→
02/16 17:09, , 9F
02/16 17:09, 9F
→
02/16 17:09, , 10F
02/16 17:09, 10F
→
02/16 17:09, , 11F
02/16 17:09, 11F
→
02/16 17:10, , 12F
02/16 17:10, 12F
→
02/16 17:22, , 13F
02/16 17:22, 13F
推
02/16 17:33, , 14F
02/16 17:33, 14F
推
02/16 17:36, , 15F
02/16 17:36, 15F
→
02/17 10:15, , 16F
02/17 10:15, 16F
→
02/17 10:15, , 17F
02/17 10:15, 17F
→
02/17 10:15, , 18F
02/17 10:15, 18F
→
02/17 10:17, , 19F
02/17 10:17, 19F
→
02/17 10:21, , 20F
02/17 10:21, 20F
→
02/17 10:22, , 21F
02/17 10:22, 21F
→
02/17 10:22, , 22F
02/17 10:22, 22F
→
02/17 10:24, , 23F
02/17 10:24, 23F
→
03/10 14:16, , 24F
03/10 14:16, 24F