1 客戶首先使用VB進行編輯表單,生成一個apply.xml文件。
在VB中,使用MSXML 4.0。如果不設定編碼方式,保存的時候,文件默認就是UTF-8編碼
Set dom = CreateDOM
Set node = dom.createProcessingInstruction("xml", "version='1.0'")
dom.appendChild node
Set node = Nothing
2 客戶將這個XML通過Web上傳到服務器
在PHP中,XMLDOM只支持UTF-8作為默認編碼。所以生成的XML文件,上傳以后可以直接解析這個文件,獲得一些信息
if (!dom = domxml_open_mem(content)) {
t->assign('msg', "文件解析錯誤!");
t->render('noavailable.html', PAGE_TITLE, 'wrap.html');
exit;
}
接下來,要將這個文件存到數據庫里面,因為數據庫使用MS Sql Server,它不支持UTF-8的數據結構,所以將整個文件以二進制的方式存到數據庫里面,這里讓我搞了半天的就是二進制文件的存放方式,如果是mysql,那不需要做任何轉換就可以直接存了,但是mssql不行,原因是:
This is because the MSSQL parser makes a clear distinction between binary an character constants. You can therefore not easilly insert binary data with "column = 'data'" syntax like in MySQL and others.
The MSSQL documentation states that binary constants should be represented by their unquoted hexadecimal byte-string. That is.. to set the binary column "col" to contain the bytes 0x12, 0x65 and 0x35 you shold do "col = 0x126535" in you query.
具體操作如下:
//讀取上傳的文件
original = _FILES['content']['name'];
if (!empty(original)) {
if (_FILES['content']['type'] == "text/xml") {
filename = _FILES['content']['tmp_name'];
handle = fopen(filename, "rb");
originalcontent = fread(handle, filesize(filename));
fclose(handle);
}
} //end if(!empty(original))
originalcontent = unpack("H*hex", originalcontent); //這步是關鍵
db->query("insert into ".TBL_SB_ONLINE_USER." (sb_id, user_id, username, sbmc, content, created_date) values ("
.newid.", "
.u.", "
.db->quote(stripslashes(name)).", "
.db->quote(stripslashes(sbmc)).", 0x"
.originalcontent['hex'].", " //注意這里,前面有0x
."'now')");
3 上傳之后,用戶也可以在網上對這個文件進行在線編輯,這時需要將這個文件從數據庫讀出,然后還原成UTF-8編碼,再進行解析。雖然我們上面使用了unpack,但讀出的時候不需要還原。
sb = db->getRow('select sbmc, content from '.TBL_SB_ONLINE_USER." where sb_id = sb_id");
originalcontent =sb[content];
if (!dom = domxml_open_mem(originalcontent)) {
t->assign('msg', "文件解析錯誤!");
t->render('noavailable.html', PAGE_TITLE, 'wrap.html',true);
exit;
}
context = xpath_new_context(dom);
xpath = context->xpath_eval("//material/xm");
t->assign('xm',iconv("UTF-8","GBK",xpath->nodeset[0]->get_content()));
讀出的時候,mssql除了用于 SQL Server 的 Microsoft OLE DB 提供程序和 SQL Server ODBC 驅動程序自動將 @@TEXTSIZE 設置為最大值 2 GB。其他的都是4096 (4 KB),所以用PHP訪問時候,務必將下面打開mssql.textlimit = 2147483647
mssql.textsize = 2147483647
4 后臺用VB,要解析該函數需要添加以下代碼,用來將byte()轉換成utf-8編碼
Public Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, _
ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
Public Const CP_UTF8 = 65001
Public Function UTF8_Decode(bUTF8() As Byte) As String
Dim lRet As Long
Dim lLen As Long
Dim lBufferSize As Long
Dim sBuffer As String
Dim bBuffer() As Byte
lLen = UBound(bUTF8) + 1
If lLen = 0 Then Exit Function
lBufferSize = lLen * 2
sBuffer = String(lBufferSize, Chr(0))
lRet = MultiByteToWideChar(CP_UTF8, 0, VarPtr(bUTF8(0)), lLen, StrPtr(sBuffer), lBufferSize)
If lRet <> 0 Then
sBuffer = Left(sBuffer, lRet)
End If
UTF8_Decode = sBuffer
End Function
具體讀數據庫的操作是
Dim varcontent() As Byte
varfilesize = mrc.Fields("content").ActualSize
varcontent = mrc.Fields("content").GetChunk(varfilesize)
content = UTF8_Decode(varcontent)
xmlDoc.async = False
xmlDoc.resolveExternals = False
xmlDoc.loadXML (content)
If (xmlDoc.parseError.errorCode <> 0) Then
Dim myErr
Set myErr = xmlDoc.parseError
MsgBox ("發生錯誤 " & myErr.reason)
Else
xmlDoc.setProperty "SelectionLanguage", "XPath"
5 后臺,在Java里面就更好操作了,將讀出的數據變成byte[],然后轉換成UTF-8的字符串。
最后要說的是,PHP的確是一個非常強大的腳本語言,如果開發PHP過程中遇到難以解決,google都不容易搜到的問題,大家直接上php.net的在線文檔,文檔里面通常有很多好心人將自己的使用心得寫在上面,非常有幫助。
這樣就能解決xml編碼問題在VB,PHP,JAVA下的問題。
新聞熱點
疑難解答