Solved

Changing valu in XML node

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Suggested Courses
Course of the Month6 days, 3 hours left to enroll

626 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