PDA

View Full Version : VBA - how to update XML returned from API



wayoutwest
11-02-2016, 05:19 AM
Hello,

This is my first ever VB script and I am trying to write a module in an access database to:
- call an API to GET item data
- XML is returned
- parse the XML, and update the item barcode
- save the XML
- call API to using the XML to update the item data

This is a cut-down example of the XML returned from the API

<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<item>
<bib_data>
<bibid>2346830320002996</bibid>
</bib_data>
<holding_data>
<holdingid>2346830320002996</holdingid>
</holding_data>
<item_data>
<itemid>2346830320002996</itemid>
<barcode>ST-99001</barcode>
<creation_date>2016-05-17Z</creation_date>
<modification_date>2016-11-02Z</modification_date>
</item_data>
</item>

In the script below I can make the API call successfully, and the XML is returned in string strResp.
I can view the XML, and can select the "//item_data/barcode" node, however, I cannot figure out how to update the node value.

I would like to change the barcode from ST-99001 to be ST-22222.

Can someone help please? I have tried so many different ways to select-for-update the barcode, all unsuccessful. Am totally baffled as to how to do this :(

The code in red is the bit that is causing the problem.

Dim getURL As String
Dim strResp As String
Dim getReq As New WinHttpRequest
Dim putReq As New WinHttpRequest
Dim xmlDoc As New MSXML2.DOMDocument
'
' Call API to GET item data
'
getURL = "https:...."
getReq.Open "Get", getURL, False
getReq.Send
strResp = getReq.ResponseText
'
' Load returned XML for parsing
'
xmlDoc.loadXML strResp
xmlDoc.async = False
'
' Get current item barcode value, and set to new value
'
nodeValue = xmlDoc.documentElement.selectSingleNode("//item_data/barcode").Text
nodeValue.Text = "ST-22222"
'
' SAVE xml with new value
'
xmlDoc.Save (strResp)

'
' Call API to UPDATE item barcode
'
putURL = "https:...."
putReq.Open "PUT", putURL, False
putReq.SetRequestHeader "Content-type", "application/xml"
putReq.Send (strResp)


Thanks in advance for your help!