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

Update XML Node Value 4

Status
Not open for further replies.

QuantumDoja

Programmer
Jun 11, 2004
76
GB
Hi, I have XML as follows, how do I access a specifc product where product id = dog and update the quantity to one more?

Code:
<root>
   <product>
      <producid>cat</productid>
      <quantity>3</quantity>
      <price>1.99</price>
   </product>
   <product>
      <producid>dog</productid>
      <quantity>2</quantity>
      <price>4.99</price>
   </product>
   <product>
      <producid>mouse</productid>
      <quantity>7</quantity>
      <price>2.95</price>
   </product>
</root>

 
Hi there,
I am doing similar things with XML and I find that they are quite easy once you get the basic principles. The way I would do what you want is something like this:

(Please note that I have not compiled this and do not know if it would really run and work but I have just written it down from memory to give you a bit of a clue as to how to go about it.)

Code:
function blah(xmlFileName as String)
 Dim xmlDoc As MSXML2.DOMDocument30
 Dim list as As IXMLDOMNodeList
 Dim i as Integer
 Dim tmp as Integer
	
	Set xmlDoc = New MSXML2.DOMDocument30
	xmlDoc.Load (xmlFileName)

	' I'm assuming that there may be more than one dog
	Set list = xmlDoc.selectNodes("//product[@productid=""dog""]")
	for i = 1 to list.length
		tmp = list.item(i).attributes.getNamedItem("quantity").nodevalue
		list.item(i).attributes.getNamedItem("quantity").nodevalue = tmp + 1
	next i
	xmlDoc.save (xmlFileName)

end function

Regards
M
 
What version of MSXML are you using?
The MSXML4 documentation shows it as being a member of the IXMLDOMNodeList object.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Hi QuantumDoja,

I'm sorry you're still having trouble. To test this out I made a form that has a lable to show the name of the xml file that we are reading, a lable to show the number of products in the xml and 3 more then lables for the properties of the product that we are changing. Then I have a "Go button" when this button is clicked the dog product is selected from the xml and the quantity of it is increased. The following is the actual code that I have in the form so I'm pretty sure that this one works. :)

I'm only using MSXML v3.0 but chiph was totally right I'd remembered the name of the node list class wrong - so thanks chiph %-)

As a point of information the node select I had before would only have worked if the xml had the product id as part of the product tag like "<product productid="dog"></product>" so I'm sorry that was my mistake. But this iterative way of finding the dog item does work. Also the way you have each detail of the product as a seperate node does make things rather difficult and a bit dependent on the order of the items in the xml. This is not really a very good idea but as this shows it can work. I think you could make life a lot easier for yourself by changing your xml if you can.

Code:
Dim fso As FileSystemObject
Dim xmlDoc As MSXML2.DOMDocument30
Dim xmlFileName As String
Dim list As IXMLDOMNodeList
 
Private Sub Form_Load()
    Set fso = New FileSystemObject
    xmlFileName = App.Path & "\Pets.xml"
    If fso.FileExists(xmlFileName) Then
        FileName.Caption = xmlFileName
        Set xmlDoc = New MSXML2.DOMDocument30
        xmlDoc.Load (xmlFileName)
        GoButton.Enabled = True
    Else
        MsgBox "the file ain't there!" & vbNewLine & xmlFile
        GoButton.Enabled = False
    End If
End Sub

Private Sub GoButton_Click()
 Dim i As Integer, j As Integer
 Dim tmp As Integer, base As String
 
    ' I'm assuming that there may be more than one dog
    Set list = xmlDoc.selectNodes("//product")
    For i = 0 To list.length - 1
        ' Note that the details of the product are child nodes
        For j = 0 To list.Item(i).childNodes.length - 1
            base = list.Item(i).childNodes.Item(j).baseName
            If base = "productid" Then
                ProductId.Caption = list.Item(i).childNodes.Item(j).nodeTypedValue
            ElseIf base = "price" Then
                price.Caption = list.Item(i).childNodes.Item(j).nodeTypedValue
            ElseIf base = "quantity" Then
                ' this relys on the fact that product id is before quantity
                If ProductId.Caption = "dog" Then
                    tmp = quantity.Caption
                    list.Item(i).childNodes.Item(j).nodeTypedValue = Str(tmp + 1)
                    End If
                quantity.Caption = list.Item(i).childNodes.Item(j).nodeTypedValue
            Else
                MsgBox "unrecognised property found " & base
            End If
        Next j
    Next i
    xmlDoc.save (xmlFileName)

End Sub

let me know how you get on
M

'Here I am, brain the size of a planet and they ask me to take you down to the bridge. Call that "job satisfaction"? Cos I don't.'
 
marvelous.. just what ive been looking for *

If somethings hard to do, its not worth doing - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top