Link to home
Start Free TrialLog in
Avatar of kenozzy
kenozzyFlag for United States of America

asked on

Getting raw XML in ASP Vbscript

I am using ASP Vbscript to receive XML on an IIS webserver.  Occasionally we get invalid XML that we would like to debug by looking at the raw data.  However, while we have been successful receiving valid XML, I can't find a way to save corrupt or invalid XML.

We are using the following code to load the XML.
Set x = Server.CreateObject("MSXML2.DOMDocument")
x.setProperty "ServerHTTPRequest", True
x.async = False
x.load(Request)

I'd like to save everything that comes into a file, so that if there are problems, I can look at the XML and debug it.

Please help!
ASKER CERTIFIED SOLUTION
Avatar of zc2
zc2
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kenozzy

ASKER

Ok, great.  Thanks!

But if I first read the XML using BinaryRead and save the file, how do I then load it into DOMDocument to check if it is valid?
Avatar of sybe
sybe

You could do a regular GET to the server like with this function. It returns a String.

Function ReadUrl(ByVal sUrl)
	Dim oXMLHttpRequest
	Set oXMLHttpRequest = Server.CreateObject("Microsoft.XMLHTTP")
	oXMLHttpRequest.open "GET", sUrl, False
	oXMLHttpRequest.send
	ReadUrl = oXMLHttpRequest.responseText
	Set oXMLHttpRequest = Nothing
End Function

Open in new window


The problem might also be that there is a time-out. It is possible to catch that too.
Just load not from the Request but from that variable:
x.load(vntPostedData)
> how do I then load it into DOMDocument to check if it is valid?

sXML = ReadUrl("http://www.domain.com/webservice")

Set x = Server.CreateObject("MSXML2.DOMDocument")
x.async = False
x.loadXML(sXML)


Function ReadUrl(ByVal sUrl)
	Dim oXMLHttpRequest
	Set oXMLHttpRequest = Server.CreateObject("Microsoft.XMLHTTP")
	oXMLHttpRequest.open "GET", sUrl, False
	oXMLHttpRequest.send
	ReadUrl = oXMLHttpRequest.responseText
	Set oXMLHttpRequest = Nothing
End Function

Open in new window

BTW, if you need to conver that byte array to string, here's a function which can do the conversion:
Function StrConv(ByRef Source, ByVal ToString)
  Dim Stream
  Const adTypeBinary = 1
  Const adTypeText = 2

  Set Stream = CreateObject("ADODB.Stream")
  With Stream
    .Open
    If ToString Then
      .Type = adTypeBinary
      .Write Source
      .Position = 0
      .Type = adTypeText
      .Charset = "ascii"
      StrConv = .ReadText()
    Else
      .Type = adTypeText
      .Charset = "ascii"
      .WriteText Source
      .Position = 0
      .Type = adTypeBinary
      StrConv = .Read()
    End If
    .Close
  End with
End Function

Open in new window

Use it as follows:
str =  StrConv( vntPostedData, true )
Avatar of kenozzy

ASKER

That's perfect, thanks
kenozzy, are you sure you have accepted the right solution?
Avatar of kenozzy

ASKER

Very sorry Zc2... your solution was the correct one.  

I will request a change.
Avatar of kenozzy

ASKER

Excellent thanks.
Thank you.