Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Changing valu in XML node

Posted on 2001-07-23
5
Medium Priority
?
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 8

Accepted Solution

by:
Dave_Greene earned 200 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

704 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