|
-
Jan 30th, 2013, 01:58 PM
#1
Thread Starter
New Member
Help using VB Script to edit an XML File
So work gave me a project. I'm a novice programmer at best (just enough to pass the two Java Programming courses required by my degree), and I'm more of a hardware and troubleshooting guy. So I'm here asking for some assistance.. bacon needs saving here... I'm trying to edit this XML File, specifically the tags /accounts/a0/name and /accounts/a0/jid.
Code:
<!DOCTYPE accounts>
<accounts version="0.14" xmlns="http://psi-im.org/options">
<accounts>
<a0>
<tls>
<override-certificate type="QByteArray">
</override-certificate>
<override-domain type="QString">
</override-domain>
</tls>
<custom-auth>
<use type="bool">false</use>
<authid type="QString">
</authid>
<realm type="QString">
</realm>
</custom-auth>
<log type="bool">true</log>
<use-host type="bool">false</use-host>
<stun-port type="QString">3478</stun-port>
<security-level type="int">0</security-level>
<port type="int">5222</port>
<ignore-SSL-warnings type="bool">false</ignore-SSL-warnings>
<keep-alive type="bool">true</keep-alive>
<connect-after-sleep type="bool">true</connect-after-sleep>
<pgp-secret-key-id type="QString">
</pgp-secret-key-id>
<name type="QString">Default</name>
<ssl type="QString">auto</ssl>
<reconn type="bool">true</reconn>
<proxy-id type="QString">
</proxy-id>
<bytestreams-proxy type="QString">
</bytestreams-proxy>
<priority type="int">5</priority>
<jid type="QString">[email protected]</jid>
<resource type="QString">Psi</resource>
<password type="QString">
</password>
<compress type="bool">false</compress>
<automatic-resource type="bool">true</automatic-resource>
<legacy-ssl-probe type="bool">true</legacy-ssl-probe>
<require-mutual-auth type="bool">false</require-mutual-auth>
<enabled type="bool">true</enabled>
<auto type="bool">true</auto>
<allow-plain type="QString">over encryped</allow-plain>
<stun-host type="QString">
</stun-host>
<host type="QString">
</host>
</a0>
</accounts>
</accounts>
I need to edit the text of /accounts/a0/name to be the username currently logged onto the windows machine, and the text of /accounts/a0/jid to read <username>@abc.123.com. I need the script to make these changes, then reach out and start an executable in the program files. This is what I have so far.
Code:
Dim strPsiUser
Set oWshShell = WScript.CreateObject ("WScript.Shell")
strPsiUser = oWshShell.ExpandEnvironmentStrings ("%UserName%")
Set xmlDoc = _
CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
xmlDoc.Load("C:\My Documents\accounts.xml")
xmlDoc.Save "C:\My Documents\accounts.xml"
I've not had luck with code to set the text, and everything I have tried to save the file ends up with a blank file. I would appriciate any assistance you could offer.
Thank You!
-
Feb 3rd, 2013, 09:58 AM
#2
Re: Help using VB Script to edit an XML File
This should handle the xml part.
One thing I see with your xml is the doctype node is incomplete and will cause the load of your file to fail. Either remove the node or make sure it is complete.
<!DOCTYPE accounts>
Code:
Option Explicit
Dim xmlDoc, objNode
Dim strFile
Dim strUserName, strID
strUserName = "MyUserName"
strID = strUserName & "@abc.123.com"
' Path to your xml file
strFile = "C:\My Documents\accounts.xml"
' Create an xml Documment object and load your file
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
xmlDoc.Load strFile
' Find the accounts/a0/name node
Set objNode = xmlDoc.SelectSingleNode("accounts/accounts/a0/name")
' Test that a node was returned
If Not objNode Is Nothing Then
objNode.Text = strUserName
End If
' Find the accounts/a0/jid node
Set objNode = xmlDoc.SelectSingleNode("accounts/accounts/a0/jid")
' Test that a node was returned
If Not objNode Is Nothing Then
objNode.Text = strID
End If
' Save the changes
xmlDoc.Save strFile
' Clean up
If Not objNode Is Nothing Then
Set objNode = Nothing
End If
Set xmlDoc = Nothing
-
Feb 3rd, 2013, 05:27 PM
#3
Thread Starter
New Member
Re: Help using VB Script to edit an XML File
Thank you for the response.
I was not aware that the XML document declaration was incomplete. It is generated by the program, so I'm not sure if it's possible to change it. I'll experiment with it and thank for this code! Bacon is saved, if only I could send you a BLT now!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|