Solved

Changing valu in XML node

Posted on 2001-07-23
5
235 Views
Last Modified: 2012-06-22
I have the following sample xml below. There are 2 child nodes called "book" under the parent "books" node.  Within the book node is a node called "book_id".  Under the first book node, there is no value for the book_id element and there is a value for book_id under the 2nd book node. I want to insert a value for the book_id element if there are no values in that element. I also want to be able to change the value of the 2nd book_id element that has a value.  Here is what I need to do

1) Find the book_id element that has no value, if this is the case, then add a value that has been stored in a variable.

2) Find the book_id element that has a value, if there is a value, then change it with a value that has been stored in a variable

Below is the xml I want to change:

<books>
  <book>
  <book_id>no value</book_id>
  </book>
 
  <book>
  <book_id>10</book_id>
  </book>
</books>




I want the above xml to become the xml below:


<books>
  <book>
  <book_id>5</book_id>
  </book>
 
  <book>
  <book_id>20</book_id>
  </book>
</books>


0
Comment
Question by:tinman1412
  • 2
  • 2
5 Comments
 
LVL 8

Accepted Solution

by:
Dave_Greene earned 50 total points
ID: 6310180
Option Explicit

Dim oDOM      As DOMDocument
Dim oRoot     As IXMLDOMNode
Dim oNode     As IXMLDOMNode
Dim oNodeList As IXMLDOMNodeList

Private Sub Command1_Click()

 Set oDOM = New DOMDocument
 Dim i As Long
 
 oDOM.Load "C:\One.xml"

 Set oRoot = oDOM.selectSingleNode("books")
 
 Set oNodeList = oRoot.selectNodes("book")
 
 For i = 0 To oNodeList.Length - 1
   Set oNode = oNodeList.Item(i)
   Set oUpdateNode = oNode
   If oUpdateNode.Attributes.getNamedItem("book_id").Text = "" Then
     oUpdateNode.Attributes.getNamedItem("book_id").Text = 5
   Else
     oUpdateNode.Attributes.getNamedItem("book_id").Text = 20
   End if
   
   ' replace old node with updates
   oRoot.replaceChild oUpdateNode, oNode
                 
 
End Sub

Private Sub Form_Unload(Cancel As Integer)
 Set oDOM = Nothing
 Set oNode = Nothing
 Set oNodeList = Nothing
End Sub
0
 

Author Comment

by:tinman1412
ID: 6310240
There is an error on the first "if" statement. Error '91', Object variable or with block not set
0
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6310271
Add the code between << ... >>

Set oNodeList = oRoot.selectNodes("book")

<<
If oNodeList is nothing then
  msgbox "Nodes not found"
  Exit sub
end if
>>

For i = 0 To oNodeList.Length - 1

___________________

If you get "Nodes Not Found" error, you will need to post your XML here.


0
 
LVL 3

Expert Comment

by:Hornet241
ID: 6315098

Dim XMLDoc As DOMDocument

XMLDoc.loadXML XMLStr   ' if XML is a string
'    Or
XMLDoc.load "C:\Source.xml"   ' if XML is in a file

Set XMLRoot = XMLDoc.documentElement

Set XMLBooks = XMLRoot.getElementsByTagName("Books")
Set XMLBook = XMLBooks.getElementsByTagName("Book")
XMLBook.Item(0).getElementsByTagName("Book_Id").Item(0).Text = "5"

XMLBook.Item(1).getElementsByTagName("Book_Id").Item(0).Text = "20"

NewXMLStr = XMLDoc.xml ' If string wanted
'    Or
XMLDoc.Save "C:\Destination.xml"  ' not sure of syntax here and don't have VB where I currently am.  Check MSDN help on 'DOMDocument'

0
 
LVL 3

Expert Comment

by:Hornet241
ID: 6315122
Oops!

Change this
     Set XMLBook = XMLBooks.getElementsByTagName("Book")
to this
     Set XMLBook = XMLBooks.Item(0).getElementsByTagName("Book")

OR

Change this
     Set XMLBooks = XMLRoot.getElementsByTagName("Books")
to this
     Set XMLBooks = XMLRoot.getElementsByTagName("Books").Item(0)


0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now