This article mainly introduces the precautions when using MSXML2.DOMDocument to process XML data in asp. This article gives 4 issues that need attention. Friends in need can refer to the following
When asp uses MSXML2.DOMDocument to load xml files on the network, you need to pay attention to the settings before calling the load method:
Copy the code code as follows:oXML.setProperty ServerHTTPRequest, true
That is, if the ServerHTTPRequest request is enabled, a system error will not occur: -2146697209.
When using the MSXML2.DOMDocument component in asp, you need to pay attention to the following 4 situations:
Copy the code code as follows:<%
Dim oXML, oXMLError, ReturnValue, x
Set oXML = Server.CreateObject(MSXML2.DOMDocument)
' Set whether DOMDocument loads xml files asynchronously or synchronously
oXML.async = false'==========A
' Setting purpose: True/False, indicating whether to use the server-safe ServerXMLHTTP component to load xml documents
oXML.setProperty ServerHTTPRequest, true'==========B
'Load network xml file via HTTP
ReturnValue = oXML.Load(http://localhost/asp/ServerXML.xml)'==========C
'Or load the server's local xml file
'ReturnValue = oXML.Load(d:/test.xml)'==========D
' Print the return value of DOMDocument.load:
Response.write Result of load method is = & ReturnValue & <br>
%>
First case (loading empty document):
By default, the async attribute of the DOMDocument object is TRUE, which means it is loaded asynchronously. And the ServerXMLHTTP component is not used when loading. That is, comment out lines A and B in the above code.
Copy the code code as follows:'oXML.async = false'==========A
'oXML.setProperty ServerHTTPRequest, true'==========B
At this time, the loading will be successful. ReturnValue will be TRUE. But the XML document loaded into the DOMDocument is empty! In other words, the TRUE returned by the load method is a cover! The result looks like this: Programmers must be careful in this case! Although the XML does not report any errors, and the load method also indicates success, the xml attribute of the DOM is empty.
The second case (error reason: -2146697209):
If you explicitly declare the async attribute to FALSE, the XML document is loaded synchronously. And the ServerXMLHTTP component is not used when loading. That is, only comment out line B in the above code.
Copy the code code as follows:oXML.async = false'==========A
'oXML.setProperty ServerHTTPRequest, true'==========B
Then you will encounter failure! ReturnValue will be False. The error reason is: -2146697209 - No data available for the required resource. In other words, synchronously loading XML resources on the Internet will not succeed! !
Third case (not allowed):
If your async attribute is TRUE, the XML document is loaded asynchronously. And use the ServerXMLHTTP component when loading. That is, enable line B in the code above. Comment out line A of code.
Copy the code code as follows:'oXML.async = false'==========A
oXML.setProperty ServerHTTPRequest, true'==========B
Reported a bug! Note that using SXH components to asynchronously load XML documents is not allowed! The error description is: -1072897486 - The ServerHTTPRequest property can not be used when loading a document asynchronously and is only supported on Windows NT 4.0 and above.
The fourth case (correct use):
If you explicitly declare the async attribute to FALSE, the XML document is loaded synchronously. And use the ServerXMLHTTP component when loading. That is, enable lines A and B in the code above.
Copy the code code as follows:oXML.async = false'==========A
oXML.setProperty ServerHTTPRequest, true'==========B
That’s it! It shows that using SXH component to load XML documents can correct the error of asynchronous loading of Internet resources.
Summarize
Loading method | Whether to use ServerXMLHTTP component | Load results |
Asynchronous loading | Not used | Loading will be successful. But the XML document loaded into the DOMDocument is empty |
Synchronous loading | Not used | Will encounter failure! The error reason is: -2146697209 - No data available for the required resource. |
Asynchronous loading | use | not allowed |
Synchronous loading | use | truly successful |