Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Update XML file with VBScript

Posted on 2009-06-29
4
Medium Priority
?
1,681 Views
Last Modified: 2012-08-14
I am trying to use an XML file as a sort of ini file for a vbscript hta I am writing. I am new to writing/reading xml so I found some code on here to read xml in to variables. I figured out how to use it the code for what I need, and now I am trying to figure out how to save data back to the xml file.  

I've added the code from my xml file and config file  to show what I am doing.  What I assistance with is, for example, if someone changes the oldfiles variable to c:\old  it will save that back to the oldfiles element in the xml file.   Thanks !
The vbscript: (also attached as vbscript.txt)
 
Dim xml_oldfiles, xml_dhcpserver,xml_dc,xml_domain,xml_dadmin,xml_ladmin,xml_docsdest
 
Dim oXml
Set oXml = CreateObject("Msxml2.DOMDocument")
oXml.async = False
oXml.load(".\config.xml")
 
Dim oNodeList, oNode
Set oNodeList = oXml.selectNodes("/helionizer/config")
For Each oNode in oNodeList
 
     xml_oldfiles = ProtectedGet(oNode.selectSingleNode("oldfiles"))
     xml_dc = ProtectedGet(oNode.selectSingleNode("dc"))
     xml_domain = ProtectedGet(oNode.selectSingleNode("domain"))
     xml_dadmin = ProtectedGet(oNode.selectSingleNode("dadmin"))
 
  
Next
msgbox (xml_oldfiles & " " & xml_dc & " " & xml_domain & " " & xml_dadmin)  
 
Function ProtectedGet(ByVal voNode)
	If not voNode Is Nothing Then ProtectedGet = voNode.text
End Function
 
The xml: (also attached as config.txt)
<helionizer>
<config>
<oldfiles>c:\oldfiles</oldfiles>
<dc>dc</dc>
<domain>domain.local</domain>
<dadmin>Administrator</dadmin>
</config>
</helionizer>

Open in new window

vbscript.txt
config.txt
0
Comment
Question by:heliontech
[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
4 Comments
 
LVL 8

Accepted Solution

by:
jwarnken earned 2000 total points
ID: 24737338
here is a sample that I use to update xml
ConfigXML = "D:\Repository.xml"
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
LoadXML(ConfigXML)
Set colMfiles = xmlDoc.selectNodes("/Repository/ManagedFiles/File[@Type='CallIn']")
For Each t In colMfiles
	WScript.Echo "Current test for Lane is : " & t.text
	If t.text <> "C:\program files\Test.xml" Then
		WScript.Echo "Updating Menusys for CallIn Lane"
		t.text = "C:\program files\Test.xml"
	Else 
		WScript.Echo "No Update needed for test Lane"
	End If 
Next
 
WScript.Echo "Adding/updating test lane to rcpt_fuel"
Set oRoot = xmlDoc.selectSingleNode("/Repository/ManagedFiles/rcpt_fuel/File[@Type='CallIn']")
if oRoot is nothing Then
	WScript.Echo "CallIn was not defined adding node to xml"
	Set newRoot = xmlDoc.selectSingleNode("/Repository/ManagedFiles/rcpt_fuel")
	Set oRoot = xmlDoc.createElement("File")
	oRoot.text = "C:\program files\Data\rcpt_fuel.bmp.qsr"
	set objAttr = xmlDoc.createAttribute("Type")
	objAttr.Text = "CallIn"
	Call oRoot.attributes.setnamedItem( objAttr )
	newRoot.appendChild(oRoot)
Else
	WScript.Echo "CallIn was found."
	If oRoot.text <> "C:\program files\Data\rcpt_fej_tfuel.bmp.qsr" Then
		WScript.Echo "Updating rcpt_fuel.bmp for CallIn lane."
		oRoot.text = "C:\program files\Data\rcpt_fej_tfuel.bmp.qsr"
	Else 
		WScript.Echo "No updated needed for rcpt_fuel.bmp for CallIn lane."
	End If 
end If
 
 
WScript.Echo "Saving File"
xmlDoc.save(ConfigXML)

Open in new window

0
 

Author Comment

by:heliontech
ID: 24737999
Ahhh ok I think I was missing that I had to set it.  I tried the code I have now and it worked perfectly.  Is the way I have it going to be the best way to go about it ?  Just setting a variable to the xml element updating the text and then moving on to the next ?   I just want to make sure it doesn't bog down my script if I have to change say 30 elements.


sub saveconfig
 
Dim oXml
Set oXml = CreateObject("Msxml2.DOMDocument")
oXml.async = False
oXml.load(".\config.xml")
 
Set oldfiles_xml = oXml.selectsingleNode("/helionizer/config/oldfiles")
oldfiles_xml.text = "C:\oldfiles2"
 
oxml.save(".\config.xml")
end sub

Open in new window

0
 
LVL 8

Expert Comment

by:jwarnken
ID: 24738478
You are selecting the node and not looping to find it so you should be ok. There may be a more efficient way but unless you are doing 1000s of updates I think you will be ok
0
 

Author Closing Comment

by:heliontech
ID: 31597948
Thanks !
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

604 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