INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Split XML data in VBScript

Split XML data in VBScript

(OP)
Hello, I'm able to receive and display my XML message using the following (in my .asp file):

CODE

Response.ContentType = "text/xml"
Response.Write(httpRequest.ResponseText) 

The message arrives in normal XML format:

CODE

<ChargeList>
<Charge>
<Name>Fare</Name>
<Currency>GBP</Currency>
<Price>259.29</Price>
</Charge>
<Charge>
<Name>Waiting Time</Name>
<Currency>GBP</Currency>
<Price>0.00</Price></Charge>
</ChargeList> 

I would like to create an array and assign each item in the XML list to a variable which I can then use throughout my ASP page.

Can somebody please assit me?

Thanks
Antony

RE: Split XML data in VBScript

(OP)
That looks very useful. Thank you. Let me get my teeth into that.

RE: Split XML data in VBScript

(OP)
Ok I have been trying various combinations all morning and cannot find a solution.

This is my code:

CODE

<%

Dim stringXML, httpRequest, postResponse

stringXML = "<?xml version=""1.0"" encoding=""UTF-8""?>"
stringXML = stringXML & "<SOAPENV:Envelope "
stringXML = stringXML & "xmlns:SOAPENV=""http://www.w3.org/2003/05/soap-envelope"" "
stringXML = stringXML & "xmlns:SOAPENC=""http://www.w3.org/2003/05/soap-encoding"" "
stringXML = stringXML & "xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"""
stringXML = stringXML & "xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">"
stringXML = stringXML & "<SOAPENV:Body>"
stringXML = stringXML & "<StateRequest xmlns=""*MY-URL*"">"
stringXML = stringXML & "<SourceSystem>*MY-SYSTEM*</SourceSystem>"
stringXML = stringXML & "<SourcePassword>*MY-PASSWORD*</SourcePassword>"
stringXML = stringXML & "<SourceJobList>"
stringXML = stringXML & "<SourceJobID></SourceJobID>"
stringXML = stringXML & "</SourceJobList>"
stringXML = stringXML & "</StateRequest>"
stringXML = stringXML & "</SOAPENV:Body>"
stringXML = stringXML & "</SOAPENV:Envelope>"

Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")

httpRequest.Open "POST", "*MY-SERVER*", False
httpRequest.SetRequestHeader "Content-Type", "text/xml"
httpRequest.SetRequestHeader "Content-Length", Len(stringXML)
httpRequest.Send stringXML

If httpRequest.status = 200 Then
Response.ContentType = "text"
myXML = httpRequest.ResponseText
Else
Response.Redirect "http://www.google.com"
End If

Response.Write myXML

%> 

This code displays my XML message (held in myXML variable) which can be seen here:

Link

From this XML list I need to grab the TargetJobID.

I only want to display the TargetJobID value (not the actual XML message itself).
I need to read the XML message, grab the TargetJobID and only display that value.

I have tried various filters:

CODE

myFILTER = Filter(myXML,"TargetJobID") 

and I have also tried:

CODE

myVAR = myXML.getElementsByTagName("TargetJobID") 

but neither of these are working.

I'm not sure what they best way to proceed is.

Thank you in advance for any help.

RE: Split XML data in VBScript

You may want to consider replacing the escaped quote marks with ..." & chr(34) "... instead of ""


so

CODE --> vbscript

stringXML = "<?xml version=""1.0"" encoding=""UTF-8""?>" 

becomes

CODE --> vbscript

stringXML = "<?xml version=" & chr(34) & "1.0" & chr(34) & " encoding=" & chr(34) & "UTF-8" & chr(34) & "?>" 

It makes debugging simpler and reduces the chances of "Unterminated string" and such like errors.


Also; for XML the ContentType should be "text/xml" rather than just "text" which would be treated as a plain text file.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: Split XML data in VBScript

Something like this should work for you (not tested).

CODE

Set oxmlhttp = New MSXML2.ServerXMLHTTP
        With oxmlhttp
            .Open "post", sURL, False
            .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
            .send sRequest
        End With
        Set oXMLDoc = CreateObject("Msxml2.DOMDocument")
        oXMLDoc.async = False
        result = oXMLDoc.loadXML(oxmlhttp.responseText)
        myVAR = oXMLDoc.selectSingleNode("//XXXXXXXXX/XXXXX").Text 

The //XXXXXXXXX/XXXXX is Xpath.

https://msdn.microsoft.com/en-us/library/Aa926473....

You can select the text of that particular node in your XML.

I hope this helps.

Thanks.

Swi

RE: Split XML data in VBScript

Sorry, ignore the New MSXML2.ServerXMLHTTP at the top. Copied from one of my old VB6 apps. You could use your syntax in that case.

Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")

Swi

RE: Split XML data in VBScript

(OP)
Hi Swi, I'm not sure whether I need to put in all of that code or whether I have most of it already...

Would I only need the part which grabs the ID, eg:

CODE

myVAR = myXML.selectSingleNode("TargetJobID").Text 

I already have my XML messaged saved as myXML...




Chris, thanks, comments noted.

RE: Split XML data in VBScript

(OP)
Also Chris the reason I changed text/xml to text was because whenever there was an error the page would simply display as an XML document and give an XML error rather than the ASP error.

You can see here that there is an error in the ASP but it always just displays the XML error:

Link

RE: Split XML data in VBScript

You would just need this part of my code. I am not sure of the structure of the returned XML. Can you paste a sample of what XML is returned?

CODE

Set oXMLDoc = CreateObject("Msxml2.DOMDocument")
        oXMLDoc.async = False
        result = oXMLDoc.loadXML(httpRequest.ResponseText)
        myVAR = oXMLDoc.selectSingleNode("//XXXXXXXXX/XXXXX").Text 

Swi

RE: Split XML data in VBScript

I should have further explained since XML is being retrieved the top few lines are loading the XML in the DOM parser and then we are using XPath to parse out the required field into myVar.

Swi

RE: Split XML data in VBScript

(OP)
To see the exact layout of the XML please see this link:

Link

I only need to extract the TargetJobID value from this list and then assign it to myVAR

Thanks

RE: Split XML data in VBScript

Quote:

simply display as an XML document and give an XML error rather than the ASP error.

So is the XML and the ASP script in the same document?

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: Split XML data in VBScript

(OP)
Hi Chris, I think they are possibly both one document.

I'm not sure how to simply read the XML message, grab that TargetJobID and then move it to a separate ASP page.

RE: Split XML data in VBScript

Quote:

Hi Chris, I think they are possibly both one document.

Okay then, so how is the ASP script supposed to read a URL that has no data until the ASP script completes?

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: Split XML data in VBScript

(OP)
Ok so are you saying I need to load the URL, grab the item I want and then post that value to an ASP page and display it there?

RE: Split XML data in VBScript

I tested this in VB6 and working great when I downloaded your XML file.

CODE

Dim objXML As New MSXML2.DOMDocument
Dim xPE As MSXML2.IXMLDOMParseError

Private Sub Command1_Click()
        Set objXML = New MSXML2.DOMDocument
        objXML.setProperty "ProhibitDTD", True
        objXML.async = False
        result = objXML.Load("C:\Test\state.xml")
        If result = False Then
            Set xPE = objXML.parseError
            With xPE
              MsgBox "*****Error #: " & .errorCode & ": " & xPE.reason & _
                "Line #: " & .Line & vbCrLf & _
                "Line Position: " & .linepos & vbCrLf & _
                "Position In File: " & .filepos & vbCrLf & _
                "Source Text: " & .srcText & vbCrLf & _
                "Document URL: " & .url
            End With
        End If
        MsgBox objXML.selectSingleNode("//TargetJobID").Text
End Sub 

Use LoadXML, not load, to load XML data from a string. Load is for loading XML data from a file as in my example above.

Swi

RE: Split XML data in VBScript

Yes, your code that creates the XML data has to be on a separate URL to the code that is going to read it. That way the script that is for reading the XML and extracting the data, calls the script that creates the XML on a "Just in time" basis.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: Split XML data in VBScript

axLW,

Did you get this working?

Swi

RE: Split XML data in VBScript

(OP)
Not yet, I do plan on trying to get it working on Monday after I deal with my timezone/date issue in the other post..

RE: Split XML data in VBScript

(OP)
Ok I can return a valid XML message with the following URL

[link "http://www.londonlutoncars.com/statexml.asp]Link[/link]

I have used that URL in my code (with loadXML) and created a new asp page called newstatexml.asp

You can see the page here, getting an error straight away:

Link

code below:

CODE

<%

Dim objXML As New MSXML2.DOMDocument
Dim xPE As MSXML2.IXMLDOMParseError

Private Sub Command1_Click()
        Set objXML = New MSXML2.DOMDocument
        objXML.setProperty "ProhibitDTD", True
        objXML.async = False
        result = objXML.LoadXML("http://www.londonlutoncars.com/statexml.asp")
        If result = False Then
            Set xPE = objXML.parseError
            With xPE
              MsgBox "*****Error #: " & .errorCode & ": " & xPE.reason & _
                "Line #: " & .Line & vbCrLf & _
                "Line Position: " & .linepos & vbCrLf & _
                "Position In File: " & .filepos & vbCrLf & _
                "Source Text: " & .srcText & vbCrLf & _
                "Document URL: " & .url
            End With
        End If
        MsgBox objXML.selectSingleNode("//TargetJobID").Text
End Sub 

%> 

RE: Split XML data in VBScript

Quote:

Private Sub Command1_Click()

Is vb.NET not vbscript.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: Split XML data in VBScript

Actually VB6 smile You would have to change it to VBScipt which wouldn't take much. Let us know if you need assistance.

Swi

RE: Split XML data in VBScript

axLW,

You should just be able to load the XML returned from your request in the first post and parse it with DOM retrieve the variable with XPath. I can't test on my side but this should work.

CODE

Dim objXML
Dim result

Set objXML = CreateObject("MSXML2.DOMDocument")
result = objXML.LoadXML(httpRequest.ResponseText)
If result = False Then
  response.write "Error loading XML"
else
  response.write objXML.selectSingleNode("//TargetJobID").Text
end if 

Swi

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close