Solved

Changing valu in XML node

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

739 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