PDA

View Full Version : XML Discussion



stanl
11-15-2006, 10:05 AM
That will return the zip (the 5 being the zip code node)


Matt;

I'd love to take this into it's own thread. Since RPC is a pain with firewalls, HTTP requests are more prevalent and parsing responses a real art. I agree, node numbers are useless unless you already know the response. There is a freeware Chilkat XML parser that is superb assuming your aim is to parse responses as data objects. Another freeware from xStandard has HTML Tidy built into their OCX and a property ResponseAsXML which guarantees it will be loaded into the XMLDOM. However, when applied to REST (as opposed to SOAP) the response is misleading.

What one needs is a generic pre-XML parser, but that's what they said about the Holy Grail:banghead:

I put my musings into a PDF document which I posted on another scripting BBS. It is 50kb and therefore above the limits here. Stan



I moved this to a new thread from http://www.vbaexpress.com/forum/showthread.php?t=10187 for anyone jumping in

stanl
11-15-2006, 02:18 PM
Oh, and one final word on this subject. I ran across an esoteric article about NameSpaces in XML and why xPath fails, most of which I shook my head over until the author suggested a work-around. I mentioned that [assuming oXML is the XMLDOM Object and you have loaded the HTTP ResponseText via LoadXML()]

oXML.SelectSingleNode("//Zip").Text would come back empty.

Well, if you were to remove the namespace `xmlns="urn:yahoo:maps"` prior to LoadXML() it would work fine. But who would know in advance that was mucking things up.

oXML.SelectSingleNode(`//*[local-name()="Zip"]`).Text - works

.03 Stan

mvidas
11-16-2006, 07:30 AM
I did make this a new thread.. I havent tried this yet, but what about returning the XML to a string, looking for xmlns="..." and deleting that out? Would that work for all namespace problems or just for this yahoo example?

stanl
11-16-2006, 12:01 PM
I did make this a new thread.. I havent tried this yet, but what about returning the XML to a string, looking for xmlns="..." and deleting that out? Would that work for all namespace problems or just for this yahoo example?

No. In this particular instance you would eliminate the urn: but there is also an xsi - please, join me in my sense of confusion and wonderment:beerchug: Stan

http://www.xml.com/pub/a/2004/02/25/qanda.html (http://www.xml.com/pub/a/2004/02/25/qanda.html)

mvidas
11-16-2006, 01:51 PM
Upon reading an article like that... I realize I know even less than I thought I did about xml

stanl
11-16-2006, 03:00 PM
Well here goes: pseudo-quasi-crypto code to generate selection criteria for HTTP Responses.

Assumption: Your are entered into the world of parsing xml responses from web services. You have 4 useful tools, 3 from Microsoft and one from www.xstandard.com (http://www.xstandard.com) (free download)
oHTTP = CreateObject("Microsoft.XMLHTTP")
oHTTP = CreateObject("MSXML2.XMLHTTP.4.0")
oHTTP = Createobject("WinHttp.WinHttpRequest.5.1")
oHTTP = CreateObject("XStandard.HTTP")

1. with your choice of HTTP Object initialized, you perform either a GET or POST (The 3rd on the list is the leanest and perhaps best suited for multiple requests).

2. If successful, you can pass the response from the server to a second object
oXML = CreateObject("MSXML2.DOMDocument.4.0")
oXML.Async = False
oXML.setProperty("SelectionLanguage","XPath")

3. Either you can or can't parse (NOTE: for the xStandard Object use ResponseString)


If oXML.LoadXML(oHTTP.ResponseText)
'can parse
Else
'can?t parse - call error handler
End If

4. So... what if you can parse? Let's just say at this point you wanted to just get a handle on the individual node names and write a line of code for each that you could test.


cCode = ""
For Each node In oXML.GetElementsByTagName("*")
cCode = cCode & 'oXML.SelectSingleNode(`//*[local-name()="' & node.NodeName & '"]`)' & vbcrlf
Next node


By using the local-name() method of the DOM you overcome shooting blanks due to NameSpaces in the response code, because you decided it was easier to code that way than fix the Namespaces and besides the loop above can have cCode written to file.

So using the URL that spawned this thread from another thread..
cUrl = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=YahooDemo&street=701+First+Street&city=Sunnyvale&state=CA"

you get something like

stanl
11-17-2006, 05:19 AM
:oops: I meant



cCode = ""
For Each node In oXML.GetElementsByTagName("*")
cCode = cCode & 'var' & node.NodeName & '=oXML.SelectSingleNode(`//*[local-name()="' & node.NodeName & '"]`).Text' & vbcrlf
Next node



But hopefully you get the jist, pre-code to write code snippets you can test and use in apps. Stan

stanl
11-17-2006, 10:22 AM
...and no discussion of XML is complete without mentioning SOAP...

First, go here and look at the xml
http://www.webcontinuum.net/webservices/ccydemo.wsdl

Then go here to see how it works with Excel
http://www.webcontinuum.net/ws_4.aspx

Stan