XML DOM/Null property problem (or just VBScript related(?!))
am 11.01.2007 21:46:37 von Jon L
Hi,
I'm hoping someone can help me with this problem. I'm not sure whether
the problem lies with the software or with my understanding of the
language.
I'm using the Microsoft.XMLDOM object in an ASP page to read an
incoming XML post request and respond to it. Although the XMLDOM
object verifies the XML at a basic level, I want to make sure that the
request is in the correct format (as per the specification I have to
work to), as well as making sure that any XML at all was sent
successfully.
At the moment, I'm stuck at the point of just trying to make sure that
an XML request was sent at all. Here's the code I'm using.. (note that
I'm loading the XML from a file for ease-of-testing, but am giving a
bad filename so as to create the condition where the XML didn't load
successfully):
...
<%
Dim xmlDoc
Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.Load ("C:\file_that_doesn't_exist.xml")
'xmlDoc.Load ("C:\InetPub\wwwroot\foo.xml")
If Not isNull(xmlDoc.documentElement) Then ' conditional
Response.Write xmlDoc.documentElement.nodeName
End If
%>
..
From Microsoft's DOM reference, for the 'documentElement' property
(http://msdn2.microsoft.com/en-us/library/ms759095.aspx):
"The property is read/write. It returns an IXMLDOMElement that
represents the single element that represents the root of the XML
document tree. It returns Null if no root exists."
The problem is that documentElement doesn't seem to be returning Null,
or at least the method I'm using to evaluate it is drawing the wrong
conclusion, and so the Response.Write line is always executed, and
causes an 'Object required' error on that line. (Since, I assume, it's
attempting to call 'nodeName' on a null 'documentElement')
I've tried changing the condition to:
If xmlDoc.documentElement <> Null Then
But this causes its own 'Object required' error. I've also tried
assigning the property to a variable using Set and testing the
variable, and other various variations but all to no avail!
Any comments/suggestions/corrections are extremely welcome, although
I'm looking for a way to make the conditional work as it should rather
than some other way of checking that the XML was sent properly, as I
have a more complete block of parsing code that runs into exactly the
same problem later on when checking for the existence of text values
for XML nodes.
Many thanks,
-Jon L
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Re: XML DOM/Null property problem (or just VBScript related(?!))
am 12.01.2007 07:48:56 von mmcginty
"Jon L" wrote in message
news:ee8dq2hbnq164lkm9ipgdplid68jes9ngl@4ax.com...
> Hi,
>
> I'm hoping someone can help me with this problem. I'm not sure whether
> the problem lies with the software or with my understanding of the
> language.
>
> I'm using the Microsoft.XMLDOM object in an ASP page to read an
> incoming XML post request and respond to it. Although the XMLDOM
> object verifies the XML at a basic level, I want to make sure that the
> request is in the correct format (as per the specification I have to
> work to), as well as making sure that any XML at all was sent
> successfully.
>
> At the moment, I'm stuck at the point of just trying to make sure that
> an XML request was sent at all. Here's the code I'm using.. (note that
> I'm loading the XML from a file for ease-of-testing, but am giving a
> bad filename so as to create the condition where the XML didn't load
> successfully):
That is a poor test case, you should go to the [minimal] effort of creating
a test client to post the XML sooner rather than later:
// jscript
var xml = new ActiveXObject("Microsoft.XMLDOM");
var req = new ActiveXObject("Microsoft.XMLHTTP");
xml.load(someFileName);
var Url = "http://...";
req.open("POST", Url, false);
req.send(xml);
> ..
> <%
> Dim xmlDoc
> Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
> xmlDoc.Load ("C:\file_that_doesn't_exist.xml")
> 'xmlDoc.Load ("C:\InetPub\wwwroot\foo.xml")
> If Not isNull(xmlDoc.documentElement) Then ' conditional
Uninitialized objects in VBS are represented by the value Nothing, not Null.
The confusing thing can be that a variant can be Null, and then can later be
assigned an object reference, but that doesn't apply here, documentElement
is not a variant, it is always an object type. So the test is:
If Not xmlDoc.documentElement Is Nothing Then
To check structure I like to use DOMDocument.selectNode(). For example, to
check to see if an XML object is a persisted recordset I use:
if Not xml.selectNode("xml/rs:data") Is Nothing Then
-Mark
> Response.Write xmlDoc.documentElement.nodeName
> End If
> %>
> ..
>
> From Microsoft's DOM reference, for the 'documentElement' property
> (http://msdn2.microsoft.com/en-us/library/ms759095.aspx):
>
> "The property is read/write. It returns an IXMLDOMElement that
> represents the single element that represents the root of the XML
> document tree. It returns Null if no root exists."
>
> The problem is that documentElement doesn't seem to be returning Null,
> or at least the method I'm using to evaluate it is drawing the wrong
> conclusion, and so the Response.Write line is always executed, and
> causes an 'Object required' error on that line. (Since, I assume, it's
> attempting to call 'nodeName' on a null 'documentElement')
>
> I've tried changing the condition to:
>
> If xmlDoc.documentElement <> Null Then
>
> But this causes its own 'Object required' error. I've also tried
> assigning the property to a variable using Set and testing the
> variable, and other various variations but all to no avail!
>
> Any comments/suggestions/corrections are extremely welcome, although
> I'm looking for a way to make the conditional work as it should rather
> than some other way of checking that the XML was sent properly, as I
> have a more complete block of parsing code that runs into exactly the
> same problem later on when checking for the existence of text values
> for XML nodes.
>
> Many thanks,
>
> -Jon L
>
>
> ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet
> News==----
> http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+
> Newsgroups
> ----= East and West-Coast Server Farms - Total Privacy via Encryption
> =----
Re: XML DOM/Null property problem (or just VBScript related(?!))
am 12.01.2007 14:38:03 von Martin Honnen
Jon L wrote:
> Dim xmlDoc
> Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
If xmlDoc.load("C:\file_that_doesn't_exist.xml") Then
' process loaded document here
Else
'check reason that XML was not loaded e.g.
Response.Write(xmlDoc.parseError.reason)
End If
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Re: XML DOM/Null property problem (or just VBScript related(?!))
am 14.01.2007 01:15:28 von Jon L
Mark, agreed that this was a poor test case, but it was really just to
demonstrate the problem. I'm using cURL to post an XML file for proper
testing.
The info on Null/Nothing is exactly what I was looking for, thanks.
The "If Not .. Is Nothing" syntax is what I was missing, and fixes the
problem I was having nicely.
Cheers,
-Jon L
On Thu, 11 Jan 2007 22:48:56 -0800, "Mark J. McGinty"
wrote:
>
>"Jon L" wrote in message
>news:ee8dq2hbnq164lkm9ipgdplid68jes9ngl@4ax.com...
>> Hi,
>>
>> I'm hoping someone can help me with this problem. I'm not sure whether
>> the problem lies with the software or with my understanding of the
>> language.
>>
>> I'm using the Microsoft.XMLDOM object in an ASP page to read an
>> incoming XML post request and respond to it. Although the XMLDOM
>> object verifies the XML at a basic level, I want to make sure that the
>> request is in the correct format (as per the specification I have to
>> work to), as well as making sure that any XML at all was sent
>> successfully.
>>
>> At the moment, I'm stuck at the point of just trying to make sure that
>> an XML request was sent at all. Here's the code I'm using.. (note that
>> I'm loading the XML from a file for ease-of-testing, but am giving a
>> bad filename so as to create the condition where the XML didn't load
>> successfully):
>
>That is a poor test case, you should go to the [minimal] effort of creating
>a test client to post the XML sooner rather than later:
>
> // jscript
> var xml = new ActiveXObject("Microsoft.XMLDOM");
> var req = new ActiveXObject("Microsoft.XMLHTTP");
> xml.load(someFileName);
> var Url = "http://...";
> req.open("POST", Url, false);
> req.send(xml);
>
>
>> ..
>> <%
>> Dim xmlDoc
>> Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")
>> xmlDoc.Load ("C:\file_that_doesn't_exist.xml")
>> 'xmlDoc.Load ("C:\InetPub\wwwroot\foo.xml")
>> If Not isNull(xmlDoc.documentElement) Then ' conditional
>
>Uninitialized objects in VBS are represented by the value Nothing, not Null.
>The confusing thing can be that a variant can be Null, and then can later be
>assigned an object reference, but that doesn't apply here, documentElement
>is not a variant, it is always an object type. So the test is:
>
> If Not xmlDoc.documentElement Is Nothing Then
>
>
>To check structure I like to use DOMDocument.selectNode(). For example, to
>check to see if an XML object is a persisted recordset I use:
>
> if Not xml.selectNode("xml/rs:data") Is Nothing Then
>
>
>-Mark
>
>
>> Response.Write xmlDoc.documentElement.nodeName
>> End If
>> %>
>> ..
>>
>> From Microsoft's DOM reference, for the 'documentElement' property
>> (http://msdn2.microsoft.com/en-us/library/ms759095.aspx):
>>
>> "The property is read/write. It returns an IXMLDOMElement that
>> represents the single element that represents the root of the XML
>> document tree. It returns Null if no root exists."
>>
>> The problem is that documentElement doesn't seem to be returning Null,
>> or at least the method I'm using to evaluate it is drawing the wrong
>> conclusion, and so the Response.Write line is always executed, and
>> causes an 'Object required' error on that line. (Since, I assume, it's
>> attempting to call 'nodeName' on a null 'documentElement')
>>
>> I've tried changing the condition to:
>>
>> If xmlDoc.documentElement <> Null Then
>>
>> But this causes its own 'Object required' error. I've also tried
>> assigning the property to a variable using Set and testing the
>> variable, and other various variations but all to no avail!
>>
>> Any comments/suggestions/corrections are extremely welcome, although
>> I'm looking for a way to make the conditional work as it should rather
>> than some other way of checking that the XML was sent properly, as I
>> have a more complete block of parsing code that runs into exactly the
>> same problem later on when checking for the existence of text values
>> for XML nodes.
>>
>> Many thanks,
>>
>> -Jon L
>>
>>
>> ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet
>> News==----
>> http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+
>> Newsgroups
>> ----= East and West-Coast Server Farms - Total Privacy via Encryption
>> =----
>
---
Aoccdrnig to a rscheearch at Cmabrigde Uinervtisy,
it deosn't mttaer in waht oredr the ltteers in a wrod
are, the olny iprmoetnt tihng is taht the frist and
lsat ltteer be at the rghit pclae. The rset can be
a total mses and you can sitll raed it wouthit porbelm.
Tihs is bcuseae the huamn mnid deos not raed ervey
lteter by istlef, but the wrod as a wlohe.
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----