Below is a function which adds items to an XML file for a sort of shopping cart functionality. The function determines whether the item already exists in the xml document and either updates the "qty" attribute or creates a new "item element" The problem occors when it is a new item and it tries to add a new element, specifically on the appendchild method. The error I get is "Block with not set" or something to that effect
Here is wha an XML doc would look like.
<xml version="1.0: encoding="utf-8">
<cart id="10023" storeid="1000">
<item serial="001001002" qty="2"/>
<item serial="001002011" qty="1"/>
</cart>
</xml>
The VB code is as follows:
Public Function addXMLItem(item_serial As String, quantity As Integer) As Boolean
Dim isFound As Boolean
isFound = False
If Not m_cart_id > 0 Then
If Not createNewXMLCart(m_store_id) > 0 Then
Exit Function
End If
End If
Dim xml As MSXML2.DOMDocument30
Set xml = OpenXMLCart(m_cart_id)
Set itemList = xml.getElementsByTagName("item"
For i = 0 To itemList.length - 1
If itemList(i).Attributes.getNamedItem("serial"
.nodeValue = item_serial Then
itemList(i).Attributes.getNamedItem("qty"
.nodeValue = CStr(CInt(itemList(i).Attributes.getNamedItem("qty"
.nodeValue) + quantity)
isFound = True
Exit For
End If
Next i
If Not isFound Then
Dim sCart As MSXML2.IXMLDOMNode
Dim newItem As MSXML2.IXMLDOMElement
Set sCart = xml.selectSingleNode("cart"
Set newItem = xml.createElement("item"
newItem.setAttribute "serial", item_serial
newItem.setAttribute "qty", quantity
sCart.appendChild newItem
End If
End Function
Here is wha an XML doc would look like.
<xml version="1.0: encoding="utf-8">
<cart id="10023" storeid="1000">
<item serial="001001002" qty="2"/>
<item serial="001002011" qty="1"/>
</cart>
</xml>
The VB code is as follows:
Public Function addXMLItem(item_serial As String, quantity As Integer) As Boolean
Dim isFound As Boolean
isFound = False
If Not m_cart_id > 0 Then
If Not createNewXMLCart(m_store_id) > 0 Then
Exit Function
End If
End If
Dim xml As MSXML2.DOMDocument30
Set xml = OpenXMLCart(m_cart_id)
Set itemList = xml.getElementsByTagName("item"
For i = 0 To itemList.length - 1
If itemList(i).Attributes.getNamedItem("serial"
itemList(i).Attributes.getNamedItem("qty"
isFound = True
Exit For
End If
Next i
If Not isFound Then
Dim sCart As MSXML2.IXMLDOMNode
Dim newItem As MSXML2.IXMLDOMElement
Set sCart = xml.selectSingleNode("cart"
Set newItem = xml.createElement("item"
newItem.setAttribute "serial", item_serial
newItem.setAttribute "qty", quantity
sCart.appendChild newItem
End If
End Function