473,385 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to parse xml response in VBA?

1,271 Expert 1GB
I'm working in ACCESS/VBA trying to get rates from UPS via XML. I have something that may well work but it strikes me that I may be approaching this in a very clumsy way.

I am using a series of for/next loops to loop through the nodes, and within each of those loops I have select case statements looking for individual tags. Any one tag may require drilling down into another node with for/next and select case until I get to a value I can use.

Is this really the best way to do that? I've read some things about XML processing that makes me think there's an easier way.

Here's the code. I'm sending xml to UPS and getting a response, so I'm going to start my code at the .send.

Expand|Select|Wrap|Line Numbers
  1. myHTTP.send (myXML)  '  sends rate request to ups
  2.  
  3. 'MsgBox myDom.XML
  4. Dim myNode As IXMLDOMNode
  5. Dim oDoc As MSXML2.DOMDocument
  6. Dim fSuccess As Boolean
  7. Dim oRoot As MSXML2.IXMLDOMNode
  8.  
  9.  ' =================================================
  10.     Set oDoc = New MSXML2.DOMDocument
  11.     oDoc.async = False
  12.     oDoc.validateOnParse = False
  13.     fSuccess = oDoc.Load(myHTTP.responseXML)
  14.          ' If anything went wrong, quit now.
  15.  
  16.         If Not fSuccess Then
  17.             If ynInteractive Then
  18.               MsgBox "problem loading response xml"
  19.  
  20.             End If
  21.         End If
  22.          ' Get the root of the XML tree.
  23.         Set oRoot = oDoc.documentElement
  24.  
  25.         strNodeName = ""
  26.          ' Go through all children of the root.
  27.          myXML = myHTTP.responseText
  28.  
  29.         For Each oResponseResult In oRoot.childNodes ' for each node in the root?
  30.             Select Case oResponseResult.nodeName
  31.                 Case "Response"
  32.                 Case "RatedShipment"
  33.                     Dim oRatedShipmentResultNodes As MSXML2.IXMLDOMNodeList
  34.                     Dim oRatedShipmentNode  As MSXML2.IXMLDOMNode
  35.                     strWork = ""
  36. '                    Dim oRatedShipmentResultNodes As MSXML2.IXMLDOMNodeList
  37. '                    Dim oShipmentNode  As MSXML2.IXMLDOMNode
  38.  
  39.                     Set oRatedShipmentResultNodes = oResponseResult.childNodes
  40.  
  41.                     For Each oRatedShipmentNode In oRatedShipmentResultNodes   '  for each node under "rated shipment"
  42.  
  43.                         Select Case oRatedShipmentNode.nodeName
  44.                                 Case "service"
  45.                                     ' service / code
  46.                                 Case "Ratedshipmentwarning"
  47.                                     'ratedshipmentwarning/
  48.                                 Case "BillingWeight"
  49.                                     'billingweight/unitofmeasurement/code
  50.                                     'billingweight/weight
  51.                                         Dim oBillingWeightResultNodes As MSXML2.IXMLDOMNodeList
  52.                                         Dim oBillingWeightNode  As MSXML2.IXMLDOMNode
  53.                                         Set oBillingWeightResultNodes = oRatedShipmentNode.childNodes
  54.  
  55.                                         For Each oBillingWeightNode In oBillingWeightResultNodes
  56.  
  57.                                             Select Case oBillingWeightNode.nodeName
  58.                                                 Case "UnitOfMeasurement"
  59.                                                 Case "Weight"
  60.                                                 Case Else
  61.                                                     MsgBox "oBillingWeightNode " & oBillingWeightNode.nodeName & " Not handled"
  62.                                             End Select '  oBillingWeightNode.nodeName
  63.  
  64.                                         Next  '  oBillingWeightNode In oBillingWeightResultNodes
  65.                                         Set oBillingWeightResultNodes = Nothing
  66.                                 Case "TransportationCharges"
  67.                                     'transportationcharges/currencycode
  68.                                     'transportationcharges/monetaryvalue
  69.                                         Dim oTransportChargesResultNodes As MSXML2.IXMLDOMNodeList
  70.                                         Dim oTransportChargesNode  As MSXML2.IXMLDOMNode
  71.                                         Set oTransportChargesResultNodes = oRatedShipmentNode.childNodes
  72.  
  73.                                         For Each oTransportChargesNode In oTransportChargesResultNodes
  74.                                             Select Case oTransportChargesNode.nodeName
  75.                                                 Case "CurrencyCode"
  76.                                                 Case "MonetaryValue"
  77.                                                 Case Else
  78.                                                     MsgBox "oTransportChargesNode " & oTransportChargesNode.nodeName & " Not handled"
  79.                                             End Select '  oTransportChargesNode.nodeName
  80.  
  81.                                         Next  '  oTransportChargesNode In oTransportChargesResultNodes
  82.                                         Set oTransportChargesResultNodes = Nothing
  83.  
  84.                                 Case "ServiceOptionCharges"
  85.                                     'serviceoptionscharges/currencycode/monetaryvalue
  86.                                         Dim oServiceOptionResultNodes As MSXML2.IXMLDOMNodeList
  87.                                         Dim oServiceOptionNode  As MSXML2.IXMLDOMNode
  88.                                         Set oServiceOptionResultNodes = oRatedShipmentNode.childNodes
  89.  
  90.                                         For Each oServiceOptionNode In oServiceOptionResultNodes
  91.                                             Select Case oServiceOptionNode.nodeName
  92.                                                 Case "CurrencyCode"
  93.                                                 Case "MonetaryValue"
  94.                                                 Case Else
  95.                                                     MsgBox "oServiceOptionNode " & oServiceOptionNode.nodeName & " Not handled"
  96.                                             End Select '  oServiceOptionNode.nodeName
  97.  
  98.                                         Next  '  oServiceOptionNode In oServiceOptionResultNodes
  99.                                         Set oServiceOptionResultNodes = Nothing
  100.  
  101.                                 Case "TotalCharges"
  102.                                     'totalcharges/currencycode/monetaryvalue
  103.                                         Dim oTotalChargesResultNodes As MSXML2.IXMLDOMNodeList
  104.                                         Dim oTotalChargesNode  As MSXML2.IXMLDOMNode
  105.                                         Set oTotalChargesResultNodes = oRatedShipmentNode.childNodes
  106.  
  107.                                         For Each oTotalChargesNode In oTotalChargesResultNodes
  108.                                             Select Case oTotalChargesNode.nodeName
  109.                                                 Case "CurrencyCode"
  110.                                                 Case "MonetaryValue"
  111.                                             End Select '  oTotalChargesNode.nodeName
  112.  
  113.                                         Next  '  oTotalChargesNode In oTotalChargesResultNodes
  114.                                         Set oTotalChargesResultNodes = Nothing
  115.  
  116.                                 Case "GuaranteedDaystoDelivery"
  117.                                     'Guaranteeddaystodelivery
  118.                                 Case "ScheduledDeliveryTime"
  119.                                     'ScheduledDeliveryTime
  120.                                 Case "RatedPackage"
  121.                                     'RatedPackage/TransportationCharges/CurrencyCode
  122.  
  123.                                     'RatedPackage/TransportationCharges/MonetaryValue
  124.                                     'ratedpackage/ServiceOptionsCharges/CurrencyCode
  125.                                     '                               /MonetaryValue
  126.                                     'ratedpackage/TotalCharges/CurrencyCode
  127.                                     '                       MonetaryValue
  128.                                     'ratedpackage/weight
  129.                                     'ratedpackage/billingweight/unitofmeasurement/code
  130.                                     'ratedpackage/billingweight/weight
  131.                                         Dim oRatedPackageResultNodes As MSXML2.IXMLDOMNodeList
  132.                                         Dim oRatedPackageNode  As MSXML2.IXMLDOMNode
  133.                                         Set oRatedPackageResultNodes = oRatedShipmentNode.childNodes
  134.  
  135.                                         For Each oRatedPackageNode In oRatedPackageResultNodes
  136.  
  137.                                             Select Case oRatedPackageNode.nodeName
  138.                                                 Case "TransportationCharges"
  139.                                                 Case "ServiceOptionsCharges"
  140.                                                 Case "TotalCharges"
  141.                                                 Case "Weight"
  142.                                                 Case "BillingWeight"
  143.                                                 Case Else
  144.                                                     MsgBox "RatedPackagesResult " & oRatedPackagesResultNode.nodeName & " Not handled"
  145.                                             End Select '  oRatedPackageNode.nodeName
  146.  
  147.                                         Next  '  oRatedPackageNode In oRatedPackageResultNodes
  148.  
  149.                                     Set oRatedPackageResultNodes = Nothing
  150.                                 Case Else
  151.                                     MsgBox "oRatedShipmentNode " & oRatedShipmentNode.nodeName & " Not handled"
  152.  
  153.                         End Select ' oRatedShipmentNode.nodeName
  154.  
  155.                     Next  'oRatedShipmentNode In oRatedShipmentResultNodes   '  for each node under "rated shipment"
  156.                 Case Else
  157.                     MsgBox "oResponseResult='" & oResponseResult.nodeName
  158.                 End Select 'oResponseResult.nodeName
  159.  
I've attached a text file with a sample response file from UPS (1 package) in case it's helpful.

Thanks for any advice you can offer.

Jim
Attached Files
File Type: txt myxml.txt (2.7 KB, 365 views)
Feb 3 '11 #1
0 1771

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Stephen Chell | last post by:
I'm using SAAJ 1.2 (from jwsdp-1.3) and JDK 1.4.2_03. I've written a client application that uses the SAAJ api to send a SOAP message to a servlet. The servlet successfully receives the message...
6
by: Mark | last post by:
Hi... I've come across some weird bug with Response.Cookies. Or maybe it will be called "by design" but for the life of me I can't figure out what purpose it would serve. If you're setting a...
0
by: rlm | last post by:
We are working on a first generation ASP app. I need to call URL behind the scenes. The call is restricted to HTTP GET unfortunately. Which direction should person take with .NET (using VB or...
3
by: Vijay | last post by:
Hi, In my application i have to parse through a multipart response. In my case the first part is a xml portion and second part is byte stream portion. How can i parse through the various...
3
by: Nobody | last post by:
I'm trying to put together code to deal with a SOAP with attachements response, and I'd like to process the response in a single pass. The SOAP with attachments specification returns XML in a MIME...
6
by: c676228 | last post by:
Hi everyone, I searched on the internet and didn't get exactly what I want. Do you have any? -- Betty
11
by: Paul Furman | last post by:
I'm setting up credit card payment through authorize.net and they have the option to send a POST string back to my site once complete. I'm not sure how to proceed. They don't have much to read...
6
by: =?Utf-8?B?RGF2aWRN?= | last post by:
Hello, I have an XML file generated from a third party application that I would like to parse. Ideally, I plan on having a windows service setup to scan various folders for XML files and parse the...
0
by: l034n | last post by:
I am writing a small C# application that is supposed to download the files from a given URL. When i parse the HTML, i check the headers to see if the response is "text/html" and if so, to skip the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.