On the solution of XML coding problem under Vb,php,java

Source: Internet
Author: User
Tags exit empty mssql mysql unpack xpath
xml| Coding | solve | Problems recently encountered a project, the need to save the declaration file in XML format, coding problems really let me a headache for a while. Now all unified into UTF-8 code. Specific operations in various languages

Here, I use the DOM for XML parsing, which should be simple.

1 customers first use VB to edit the form, generate a Apply.xml file.

In VB, use MSXML 4.0. If you don't set the encoding, save the file by default is UTF-8 encoding

Set dom = CreateDOM
Set node = dom.createprocessinginstruction ("xml", "version= ' 1.0 '")
Dom.appendchild node
Set node = Nothing

2 Next, the client uploads the XML to the server via the Web

In PHP, XMLDOM only supports UTF-8 as the default encoding. So the generated XML file, upload can directly parse the file, get some information

if (! $dom = Domxml_open_mem ($content)) {
$t->assign (' msg ', "File parsing error!) ");
$t->render (' noavailable.html ', page_title, ' wrap.html ');
Exit
}

Next, to save this file to the database, because the database uses MS SQL Server, it does not support the UTF-8 data structure, so the entire file in binary way into the database inside, let me do a half-day is the binary file storage mode, if it is MySQL, That does not need to do any conversion can be directly saved, but MSSQL is not good, because:

This is because the MSSQL parser makes a clear distinction between binary a character constants. You can therefore the easilly insert binary data with "column = ' $data '" syntax like in MySQL and others.

The MSSQL documentation states that binary constants should represented is their by unquoted hexadecimal. This is.. To set the binary column "col" to contain the bytes 0x12, 0x65 and 0x35 your shold do "col = 0x126535" in your query.

The specific actions are as follows:

Read the uploaded file
$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); This step is the key.

$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 ']. ","//note here, the front has 0x
."' $now ') ");

3 after uploading, the user can also online edit the file, then need to read the file from the database, and then revert to UTF-8 encoding, and then parsing. Although we use unpack above, we don't need to restore it when we read it.

$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 ', "File parsing error!) ");
$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));

When you read it, MSSQL. The Microsoft OLE DB provider and SQL Server ODBC driver for SQL Server automatically set the @ @TEXTSIZE to a maximum of 2 GB. The others are 4096 (4 KB), so when you visit with PHP, be sure to open the following mssql.textlimit = 2147483647
Mssql.textsize = 2147483647

4 Background with VB, to resolve the function need to add the following code, used to convert byte () to UTF-8 encoding

Public Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage as Long, ByVal dwflags as Long, ByVal Lpmultiby Testr 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

The specific read database operation is

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 ("Error occurred" & Myerr.reason)
Else
Xmldoc.setproperty "SelectionLanguage", "XPath"

5 Backstage, in the Java inside is better to operate, will read out the data into byte[], and then converted to a UTF-8 string.

The last thing to say is that PHP is really a very powerful scripting language, if the development of PHP process encountered difficult to solve, Google is not easy to find the problem, we directly on the php.net online documents, the document usually has a lot of good people to their own use of experience written on the above, very helpful.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.