Solved

Changing valu in XML node

Posted on 2001-07-23
5
238 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

815 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

11 Experts available now in Live!

Get 1:1 Help Now