Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split XML data in VBScript

Status
Not open for further replies.

axLW

Programmer
Feb 6, 2015
110
GB
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
 
That looks very useful. Thank you. Let me get my teeth into that.
 
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=""[URL unfurl="true"]http://www.w3.org/2003/05/soap-envelope""[/URL] "
stringXML = stringXML & "xmlns:SOAPENC=""[URL unfurl="true"]http://www.w3.org/2003/05/soap-encoding""[/URL] "
stringXML = stringXML & "xmlns:xsi=""[URL unfurl="true"]http://www.w3.org/2001/XMLSchema-instance"""[/URL]
stringXML = stringXML & "xmlns:xsd=""[URL unfurl="true"]http://www.w3.org/2001/XMLSchema"">"[/URL]
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 "[URL unfurl="true"]http://www.google.com"[/URL]
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.
 
You may want to consider replacing the escaped quote marks with ..." & chr(34) "... instead of ""


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

becomes
Code:
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
 
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-[URL unfurl="true"]www-form-urlencoded"[/URL]
            .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.


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

I hope this helps.

Thanks.

Swi
 
Sorry, ignore the [highlight #FCE94F]New MSXML2.ServerXMLHTTP[/highlight] 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
 
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.
 
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
 
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
 
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
 
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
 
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
 
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.
 
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
 
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?
 
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
 
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
 
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..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top