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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MSXML appendchild throws error

Status
Not open for further replies.

asmith555

Programmer
Oct 7, 2002
97
US
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=&quot;1.0: encoding=&quot;utf-8&quot;>
<cart id=&quot;10023&quot; storeid=&quot;1000&quot;>
<item serial=&quot;001001002&quot; qty=&quot;2&quot;/>
<item serial=&quot;001002011&quot; qty=&quot;1&quot;/>
</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(&quot;item&quot;)
For i = 0 To itemList.length - 1
If itemList(i).Attributes.getNamedItem(&quot;serial&quot;).nodeValue = item_serial Then
itemList(i).Attributes.getNamedItem(&quot;qty&quot;).nodeValue = CStr(CInt(itemList(i).Attributes.getNamedItem(&quot;qty&quot;).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(&quot;cart&quot;)
Set newItem = xml.createElement(&quot;item&quot;)
newItem.setAttribute &quot;serial&quot;, item_serial
newItem.setAttribute &quot;qty&quot;, quantity
sCart.appendChild newItem
End If
End Function
 
Do you xml variable and sCart variable have the same owner document? In other words, were they both created from the same DomDocument40?

One of the limitations of MSXML (and maybe other parsers) is that you can't copy nodes/elements from one document owner to another -- you first have to clone them and then call appendChild.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
The line that throws the error is &quot;sCart.appendChild newItem&quot;
 
Your xml is not well-formed; like this it couldnt even be loaded into a DOMdocument.
Either you meant to start with an xml-declaration. Your code would run fine. The xml would be:

<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
<cart>
<item/>
<item/>
</cart>

or you wanted to have an actual root named &quot;xml&quot;.
<xml>
<cart>
<item/>
<item/>
</cart>
</xml>
in which case you need to select:
Set sCart = xml.selectSingleNode(&quot;*/cart&quot;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top