From character documents to XML documents

Source: Internet
Author: User
Tags string version
XML Computer World Although more and more attempts are being made to speak in the Esperanto language, there are dialects such as Gb2312,shift-jis, and the use of MSXML DOM makes it clear that the inconvenience of the dialect can be realized.
I want to save the following text document directly to convert to XML, OK, the answer is NG (No good)
Strxml= "<?xml version=" "1.0" "encoding=" "GB2312" "?><xml> here are some XML documents encoded as GB2312 </XML>"
Set domxml = New DOMDocument
Domxml.loadxml strxml
If domxml.parseerror Then Debug.Print DomXML.parseError.reason Else Debug.Print Domxml.xml
Executing this code returns the switch from the current encoding to specified encoding not supported. An error message such as converting from the current encoding to the specified encoding is not supported. Why is that? Chris Lovett said: The Loadxml method always uses Unicode BSTR encoded only in UCS-2 or UTF-16. If the contents of an inactive Unicode BSTR are passed to Loadxml, the load fails. ]
So we have to load only the DOMDocument object without a "dialect" document. Remove the <?xml version= "1.0" encoding= "GB2312"?>, so that strxml will be given to the DOM for processing as a Unicode string.
Strxml= "<XML> Here are some XML documents encoded as GB2312 </XML>"
See the above usage, love their own language people may scold the MS.
If we look at this problem in depth, we can actually do the document with dialect signs loaded directly into the DOM, I made a small function to solve one of the problems.
Private Function Str2xml (Strori as String) as DOMDocument
Dim XmlDoc as DOMDocument
Set xmldoc = New DOMDocument
Dim Pi () as Byte
Pi () = StrConv (Strori, vbFromUnicode)
Xmldoc.load Pi
Set Str2xml = xmldoc
End Function
Let's try it with the following code
Set domxml = New DOMDocument
Set domxml = Str2xml (strxml)
If domxml.parseerror Then Debug.Print DomXML.parseError.reason Else Debug.Print Domxml.xml
You will find that you have correctly exported the XML document with the <?xml version= "1.0" > logo. But if you use this function to process a document without a "dialect" flag, you may receive the following error
An Invalid character is found in text Content. (There are illegal characters in the text content) I also wrote this article when I found that, I want to convert a text document into an XML document should first check the language symbol. If the dialect to my function to do, said the Esperanto let him say it.
What is the principle of the Str2xml function?
From the description of Chris Lovett below, I think it is easy to explain.
The Load method can use the following as a VARIANT:
1, (URL): If the VARIANT is BSTR, it is interpreted as a URL.
2, (Vt_array or VT_UI1): A VARIANT can also be a SAFEARRAY containing the original encoded byte.
3, (IUnknown): If the VARIANT is a IUnknown interface, the DOM document calls IPersistStreamInit for IStream, IPersistStream, and QueryInterface.
There is no mention of the non-URL application of load in MSDN, but it's just a bit of a clue in MS's case, and it's weird. My article may be called the MSXML inside article.
By the way, the data sent to the server using XMLHTTP uses the load's usage when it is translated into an XML document.
Load the posted XML data and save it to disk.
Xmldoc.load (Request);
Don't you think the use of load (Request) is strange?
In fact, we analyze the content read with Request.BinaryRead will understand. That's the safearray form of a text document, and there's nothing mysterious about it.
We suggest that you read the Chinese version of the 〈xml data encoding method.

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.