VBScript - Replace several lines in aFile
Hey guys, i gotta edit a file and replace a single line with a set of lines.
The following VB script works fine for replacing a single line.
But i have to replace the line, "OldLine" with the following:
And i need this info to be updated in the similar format, with the exact line breaks, quotes, etc.,
Can i achieve this thru VB ?
If yes, can someone help me in this....
The following VB script works fine for replacing a single line.
On Error Resume Next
Set oShell = CreateObject( "WScript.Shell" )
Set ObjFSO= CreateObject("Scripting.FileSystemObject")
Str1="OldLine"
Str2="NewLine"
GetThisPackageDOC = "C:\security.config"
Set objFile = objFSO.OpenTextFile(GetThisPackageDOC, 1)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText,Str1,Str2)
Set objFile = objFSO.OpenTextFile(GetThisPackageDOC, 2)
objFile.WriteLine strNewText
objFile.Close
But i have to replace the line, "OldLine" with the following:
<CodeGroup class="UnionCodeGroup"
version="1"
PermissionSetName="FullTrust"
Name="zzz"
Description="Grants permissions to allow zzz clients to execute inside a web browser.">
<IMembershipCondition class="UrlMembershipCondition"
version="1"
Url="http://AU88/*"/>
</CodeGroup>
</CodeGroup>
And i need this info to be updated in the similar format, with the exact line breaks, quotes, etc.,
Can i achieve this thru VB ?
If yes, can someone help me in this....
0 Comments
[ + ] Show comments
Answers (17)
Please log in to answer
Posted by:
Inabus
15 years ago
Not ideal, works and keeps it split into lines for you:
Const FORWRITING = 2
Const TEXTMODE=1
Const CREATEFILE = True
Const UNICODE = True
arrLines = Array ( _
"<CodeGroup class=""UnionCodeGroup""", _
"version=""1""", _
"PermissionSetName=""FullTrust""", _
"Name=""zzz""", _
"Description=""Grants permissions to allow zzz clients to execute inside a web browser."">", _
"<IMembershipCondition class=""UrlMembershipCondition""", _
"version=""1""", _
"Url=""http://AU88/*""/>", _
"</CodeGroup>", _
"</CodeGroup>" _
)
Set objFSO = CreateObject("Scripting.FileSystemObject")
strOldLine = "OldLine"
Set objFile = objFSO.OpenTextFile("C:\Security.config",1)
strText = objFile.ReadAll
objFile.Close
Set objFile = objFSO.OpenTextFile("C:\Security.config2",2)
i = 0
For Each strLine In arrLines
If i = 0 Then
strReplaced = strReplaced & strLine
ElseIf i = 9 Then
strReplaced = strReplaced & strLine
Else
strReplaced = strReplaced & strLine & vbCrLf
End If
i = i + 1
Next
strNewText = Replace(strText, strOldLine, strReplaced)
objFile.WriteLine strNewText
objFile.Close
Set objFile = Nothing
Posted by:
anonymous_9363
15 years ago
Now can you please help me with a more clearer explanation on hw to do this...Yes, I can.
Get an XML class file, include it in your script then set your script to write the appropriate elements/nodes/attributes/whatever. XML files are becoming more prevalent, especially for configuring applications, so learning how to manipulate them in script now will serve you in good stead in the future. Do it properly, rather than building some horredous lash-up.
EDIT:
It appears someone's done the work for you. There's a free XML component available from ChilKat. Some code can be seen here http://www.example-code.com/vbscript/xml_format.asp. The MSI containing it is here http://www.chilkatsoft.com/download/XmlActiveX.msi?bcsi_scan_DEA2E070FBFB2D7A=1
Posted by:
anonymous_9363
15 years ago
This looks like an XML file. You will really have your work cut out to do what you want in pure VBS.
There's a good XML class file knocking around which will do what you want in a much cleaner way, using the MS XML object. In addition, it will properly indent the relevant lines, making the resulting file easier to read. It handles adding elements, nodes, node attributes and so on. Google for 'VBScript +XML +class'
There's a good XML class file knocking around which will do what you want in a much cleaner way, using the MS XML object. In addition, it will properly indent the relevant lines, making the resulting file easier to read. It handles adding elements, nodes, node attributes and so on. Google for 'VBScript +XML +class'
Posted by:
rayz_0020
15 years ago
Hey Ian, Thanks for the post.
But 'm sorry tat i cudn't make out much frm tat.[:(]
Let me explain my reqmt more clearly.
I have an application wherein i will have to update an already existing file(FileName.config) on the machine with some additional info. If this cud have been a file common to all machines, i wud have included an updated file in my application and would overwrite the existing one in every machine. But here as the file content differs in each machine, i ll have to update them at run time when the app gets installed. And hence i thought of including a VbScript to achieve it.
Now can you please help me with a more clearer explanation on hw to do this...
But 'm sorry tat i cudn't make out much frm tat.[:(]
Let me explain my reqmt more clearly.
I have an application wherein i will have to update an already existing file(FileName.config) on the machine with some additional info. If this cud have been a file common to all machines, i wud have included an updated file in my application and would overwrite the existing one in every machine. But here as the file content differs in each machine, i ll have to update them at run time when the app gets installed. And hence i thought of including a VbScript to achieve it.
Now can you please help me with a more clearer explanation on hw to do this...
Posted by:
Inabus
15 years ago
Posted by:
anonymous_9363
15 years ago
Posted by:
anonymous_9363
15 years ago
Hey! Look what I found on my USB stick:
I can't recall where I got it from but I remember using it at an old client so I know it works.
Here is a (hopefully complete) implementation of the same code as a Windows Scripting Component:
BTW, if you're using Wise Package Studio, it can handle XML files. After adding an XML file, double-clicking it brings up the 'Dynamic Content' dialog. Search the WPS Help for 'XML' then see the topic 'Editing XML files During Installation'.
Lastly, I can't quite escape the feeling that this post really ought to have been in the 'Scripting' forum....LOL
Dim objXML
Dim strPath
Set objXML = New clsXML
strPath = "New.xml"
If IsObject(objXML) Then
With objXML
'.createFile strPath, "Dummy"
'Or If using an existing XML file:
.File = "New.xml"
' .createRootNodeWAttr "Profile", "version", "0.0.1"
' .createRootChild "IdInformation"
' .createRootChild "Property"
' .createRootNodeWAttr "Author", Array("type", "enable"), Array(1, 1)
' .createRootChild "UserIdentifier"
' .createChildNode "UserIdentifier","FirstName"
' .createChildNode "UserIdentifier","LastName"
' .createChildNode "UserIdentifier","FullName"
' .createChildNode "UserIdentifier","LoginID"
' .createChildNode "UserIdentifier","EmailAddress"
' .createChildNode "UserIdentifier","PhoneNumber"
' .createChildNode "UserIdentifier","Department"
' .createChildNode "UserIdentifier","NetworkEnvironment"
' .createChildNode "UserIdentifier","Group"
' .createChildNode "UserIdentifier","Notes"
' .createRootChild "GroupIdentifier"
' .createChildNode "GroupIdentifier","Name"
' .createChildNode "GroupIdentifier","Notes"
.addNodeAttribute "GroupIdentifier/Notes", "SomeKey", "SomeValue"
.updateField "UserIdentifier//LoginID", "ian.northwood"
.updateField "UserIdentifier//LoginID", "ian.northwood"
' .deleteNode("Dummy")
End With
End If
Set objXML = Nothing
'// Some samples
'objXML.createRootChild "Images"
'Here only one attribute is added to the Images/Image Node
'objXML.createChildNodeWAttr "Images", "Image", "id", "1"
'objXML.updateField "Images//Image[@id=1]", "super.gif"
'objXML.createRootNodeWAttr "Jobs", Array("Size", "Length", "Width"), Array(24, 31, 30)
'objXML.createRootNodeWAttr "Jobs", Array("Size", "Length", "Width"), Array(24, 30, 29)
'objXML.createRootNodeWAttr "Jobs", Array("Size", "Length", "Width"), Array(24, 31, 85)
'Notice that all three job nodes have size 24, all of those nodes will be updated
'objXML.updateField "Jobs[@Size=24]", "24's"
'Notice that only two nodes have the specified XPath, hence only two new child nodes will be added
'objXML.createChildNodeWAttr "Jobs[@Size=24 and @Length=31]", "Specs", Array("Wood", "Metal", "Color"), Array("Cedar", "Aluminum", "Green")
'It is always important to iterate through all of the nodes returned by this XPath query.
'For Each str In objXML.getField("Jobs[@Size=24]")
' WScript.Echo str & vbCRLF
'Next
'// End Of samples
'Response.Redirect "New.xml"
Class clsXML
'// strFile must be full path to document i.e. C:\XML\XMLFile.XML
'// objDoc is the XML Object
Private strFile
Private objDoc
'//*********************************************************************
'// Initialization/Termination
'//*********************************************************************
'// Initialize Class Members
Private Sub Class_Initialize()
strFile = ""
End Sub
'// Terminate and unload all created objects
Private Sub Class_Terminate()
Set objDoc = Nothing
End Sub
'// *********************************************************************
'// Properties
'// *********************************************************************
'// Set XML File and objDoc
Public Property Let File(strXMLFile)
Set objDoc = CreateObject("Microsoft.XMLDOM")
objDoc.async = False
strFile = strXMLFile
objDoc.Load(strFile)
End Property
'// Get XML File
Public Property Get File()
File = strFile
End Property
'// *********************************************************************
'// Functions
'// *********************************************************************
'// Create blank XML File, set current obj file to newly created file
Public Function createFile(strPath, strRoot)
Dim objFSO
Dim objTextFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.CreateTextFile(strPath, True)
With objTextFile
.WriteLine("<?xml version=""1.0"" encoding=""UTF-8""?>")
If Len(strRoot) > 0 Then
.WriteLine("<" & strRoot & "/>")
End If
.Close
End With
Me.File = strPath
Set objTextFile = Nothing
Set objFSO = Nothing
End Function
'// Get XML Field(s) based on XPath input from root node
Public Function getField(strXPath)
Dim objNodeList
Dim arrResponse()
Dim i
Set objNodeList = objDoc.documentElement.selectNodes(strXPath)
ReDim arrResponse(objNodeList.length)
For i = 0 To objNodeList.length - 1
arrResponse(i) = objNodeList.item(i).Text
Next
getField = arrResponse
End Function
'// Update existing node(s) based on XPath specs
Public Function updateField(strXPath, strData)
Dim objField
For Each objField In objDoc.documentElement.selectNodes(strXPath)
objField.Text = strData
Next
objDoc.Save strFile
updateField = True
Set objField = Nothing
End Function
'// Create node directly under root
Public Function createRootChild(strNode)
Dim objChild
Set objChild = objDoc.createNode(1, strNode, "")
objDoc.documentElement.appendChild(objChild)
objDoc.Save strFile
Set objChild = Nothing
End Function
'// Create a child node under root node with attributes
Public Function createRootNodeWAttr(strNode, attr, val)
Dim objChild
Dim objAttr
Dim i
Set objChild = objDoc.createNode(1, strNode, "")
If IsArray(attr) And IsArray(val) Then
If UBound(attr)-LBound(attr) <> UBound(val)-LBound(val) Then
Exit Function
Else
For i = LBound(attr) To UBound(attr)
Set objAttr = objDoc.createAttribute(attr(i))
objChild.setAttribute attr(i), val(i)
Next
End If
Else
Set objAttr = objDoc.createAttribute(attr)
objChild.setAttribute attr, val
End If
objDoc.documentElement.appendChild(objChild)
objDoc.Save strFile
Set objChild = Nothing
End Function
Public Function addNodeAttribute(strNode, attr, val)
Dim objNode
Dim objAttr
Dim i
Set objNode = objDoc.documentElement.selectSingleNode(strNode)
If IsArray(attr) And IsArray(val) Then
If UBound(attr)-LBound(attr) <> UBound(val)-LBound(val) Then
Exit Function
Else
For i = LBound(attr) To UBound(attr)
Set objAttr = objDoc.createAttribute(attr(i))
objNode.setAttribute attr(i), val(i)
Next
End If
Else
Set objAttr = objDoc.createAttribute(attr)
objNode.setAttribute attr, val
End If
objDoc.Save strFile
Set objAttr = Nothing
End Function
Function SetAttribute(strAttrName, strAttrValue, objNode)
Dim objAttr ' As MSXML2.IXMLDOMNode
If Not objNode Is Nothing Then
If strAttrValue <> "" or AllowEmptyAttr Then
Set objAttr = objNode.Attributes.getNamedItem(strAttrName)
If objAttr Is Nothing Then
Set objAttr = CreateAttribute(strAttrName, strAttrValue, objNode)
End If
objAttr.Text = strAttrValue
Else
RemoveAttribute strAttrName, objNode
End If
End If
End Function
'// Create a child node under the specified XPath Node
Public Function createChildNode(strXPath, strNode)
Dim objParent
Dim objChild
For Each objParent In objDoc.documentElement.selectNodes(strXPath)
Set objChild = objDoc.createNode(1, strNode, "")
objParent.appendChild(objChild)
Next
objDoc.Save strFile
Set objChild = Nothing
Set objParent = Nothing
End Function
'// Create a child node(s) under the specified XPath Node with attributes
Public Function createChildNodeWAttr(strXPath, strNode, attr, val)
Dim objParent
Dim objChild
Dim objAttr
Dim i
For Each objParent In objDoc.documentElement.selectNodes(strXPath)
Set objChild = objDoc.createNode(1, strNode, "")
If IsArray(attr) And IsArray(val) Then
If UBound(attr)-LBound(attr) <> UBound(val)-LBound(val) Then
Exit Function
Else
For i = LBound(attr) To UBound(attr)
Set objAttr = objDoc.createAttribute(attr(i))
objChild.SetAttribute attr(i), val(i)
Next
End If
Else
Set objAttr = objDoc.createAttribute(attr)
objChild.setAttribute attr, val
End If
objParent.appendChild(objChild)
Next
objDoc.Save strFile
Set objChild = Nothing
Set objParent = Nothing
End Function
'// Delete the node specified by the XPath
Public Function deleteNode(strXPath)
Dim objOld
For Each objOld In objDoc.documentElement.selectNodes(strXPath)
objDoc.documentElement.removeChild objOld
Next
objDoc.Save strFile
Set objOld = Nothing
End Function
End Class
I can't recall where I got it from but I remember using it at an old client so I know it works.
Here is a (hopefully complete) implementation of the same code as a Windows Scripting Component:
<?XML version="1.0" encoding="utf-8" ?>
<component id="XML.Server">
<?component error="true" debug="true"?>
<!--
'// Dim objXML
'// Dim strPath
'// Dim str
'//
'// Set objXML = New clsXML
'//
'// strPath = "New.xml"
'//
'// objXML.createFile strPath, "Root"
'// '// Or if you want to use an existing XML file:
'// 'objXML.File = "C:\File.xml"
'//
'//
'// objXML.createRootChild "Images"
'//
'// '// Here, only one attribute is added to the Images/Image Node
'// objXML.createChildNodeWAttr "Images", "Image", "id", "1"
'// objXML.updateField "Images//Image[@id=1]", "super.gif"
'// objXML.createRootNodeWAttr "Jobs", Array("Size", "Length", "Width"), Array(24, 31, 30)
'// objXML.createRootNodeWAttr "Jobs", Array("Size", "Length", "Width"), Array(24, 30, 29)
'// objXML.createRootNodeWAttr "Jobs", Array("Size", "Length", "Width"), Array(24, 31, 85)
'//
'// '// Notice that all three job nodes have size 24:
'// '// all of those nodes will be updated
'// objXML.updateField "Jobs[@Size=24]", "24's"
'//
'// '// Notice that only two nodes have the specified XPath:
'// '// thus, only two new child nodes will be added
'// objXML.createChildNodeWAttr "Jobs[@Size=24 and @Length=31]", "Specs", _
'// Array("Wood", "Metal", "Color"), _
'// Array("Cedar", "Aluminum", "Green")
'//
'// '// It is always important to iterate through all of the nodes
'// '// returned by this XPath query.
'// For Each str In objXML.getField("Jobs[@Size=24]")
'// WScript.Echo str & vbCRLF
'// Next
'// Set objXML = Nothing
-->
<resource id="progid">XML.Server
</resource>
<registration
description="XML.Server"
progid="XML.Server"
version="1.00"
classid="{c0b8f3ef-a4ac-4c2f-b8ca-9aa958b7dad9}">
</registration>
<public>
<method name="createFile">
<PARAMETER name="strPath"/>
<PARAMETER name="strRoot"/>
</method>
<method name="openFile">
<PARAMETER name="strXMLFile"/>
</method>
<method name="createChildNode">
<PARAMETER name="strXPath"/>
<PARAMETER name="strNode"/>
</method>
<method name="createChildNodeWAttr">
<PARAMETER name="strXPath"/>
<PARAMETER name="strNode"/>
<PARAMETER name="attr"/>
<PARAMETER name="val"/>
</method>
<method name="createRootChild">
<PARAMETER name="strNode"/>
</method>
<method name="createRootNodeWAttr">
<PARAMETER name="strNode"/>
<PARAMETER name="attr"/>
<PARAMETER name="val"/>
</method>
<method name="updateRootNodeWAttr">
<PARAMETER name="strNode"/>
<PARAMETER name="attr"/>
<PARAMETER name="val"/>
</method>
<method name="updateChildNodeWAttr">
<PARAMETER name="strNode"/>
<PARAMETER name="attr"/>
<PARAMETER name="val"/>
</method>
<method name="updateNodeWAttr">
<PARAMETER name="strNode"/>
<PARAMETER name="attr"/>
<PARAMETER name="val"/>
</method>
<method name="deleteNode">
<PARAMETER name="strXPath"/>
</method>
<method name="getField">
<PARAMETER name="strXPath"/>
</method>
<method name="updateField">
<PARAMETER name="strXPath"/>
<PARAMETER name="strData"/>
</method>
<property name="XMLFile">
<get internalname="GetXMLFileName"/>
<put internalname="SetXMLFileName"/>
</property>
</public>
<implements type="Behavior" id="Behavior"/>
<script language="VBScript">
<![CDATA[
Private objDoc
Private strMsg
Private objFSO
Private blnResult
Public strXMLFile
Function GetXMLFileName()
GetXMLFileName = strXMLFile
End Function
Function SetXMLFileName(strXML)
strXMLFile = strXML
End Function
'// Function to load XML file
Public Function openFile(ByVal strXMLFile)
Set objFSO = CreateObject("Scripting.FileSystemObject")
openFile = False
If objFSO.FileExists(strXMLFile) Then
Set objDoc = CreateObject("Microsoft.XMLDOM")
objDoc.Async = False
objDoc.Load strXMLFile
openFile = True
Else
strMsg = "File '" & strXMLFile & " does not exist."
WScript.Echo strMsg
End If
Set objFSO = Nothing
End Function
Public Function createFile(ByVal strPath, ByVal strRoot)
Dim objTextFile
createFile = False
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.CreateTextFile(strPath, True)
With objTextFile
.WriteLine("<?xml version=""1.0""?>")
.WriteLine("<" & strRoot & "/>")
.Close
End With
strXMLFile = strPath
createFile = True
Set objTextFile = Nothing
Set objFSO = Nothing
End Function
'// Get XML Field(s) based on XPath input from root node
Public Function getField(ByVal strXPath)
Dim objNodeList
Dim arrResponse()
Dim i
Set objNodeList = objDoc.documentElement.selectNodes(strXPath)
ReDim arrResponse(objNodeList.length - 1)
For i = 0 To objNodeList.length - 1
arrResponse(i) = objNodeList.item(i).Text
Next
getField = arrResponse
End Function
'// Update existing node(s) based on XPath specs
Public Function updateField(ByVal strXPath, ByVal strData)
Dim objField
updateField = False
For Each objField In objDoc.documentElement.selectNodes(strXPath)
objField.Text = strData
Next
blnResult = saveFile(strXMLFile)
If blnResult Then
updateField = True
End If
Set objField = Nothing
End Function
'// Create node directly under root
Public Function createRootChild(ByVal strNode)
Dim objChild
createRootChild = False
Set objChild = objDoc.createNode(1, strNode, "")
objDoc.documentElement.appendChild(objChild)
blnResult = saveFile(strXMLFile)
If blnResult Then
createRootChild = True
End If
Set objChild = Nothing
End Function
'// Create a child node under root node with attributes
Public Function createRootNodeWAttr(ByVal strNode, ByVal attr, ByVal val)
Dim objChild
Dim objAttr
Dim i
createRootNodeWAttr = False
Set objChild = objDoc.createNode(1, strNode, "")
If IsArray(attr) And IsArray(val) Then
If UBound(attr)-LBound(attr) <> UBound(val)-LBound(val) Then
Exit Function
Else
For i = LBound(attr) To UBound(attr)
Set objAttr = objDoc.createAttribute(attr(i))
objChild.setAttribute attr(i), val(i)
Next
End If
Else
Set objAttr = objDoc.createAttribute(attr)
objChild.setAttribute attr, val
End If
objDoc.documentElement.appendChild(objChild)
blnResult = saveFile(strXMLFile)
If blnResult Then
createRootNodeWAttr = True
End If
Set objChild = Nothing
End Function
'// Update a child node under root node with attributes
Public Function updateRootNodeWAttr(ByVal strNode, ByVal attr, ByVal val)
Dim objChild
Dim objAttr
Dim i
updateRootNodeWAttr = False
Set objChild = objDoc.selectSingleNode(strNode)
If IsArray(attr) And IsArray(val) Then
If UBound(attr)-LBound(attr) <> UBound(val)-LBound(val) Then
Exit Function
Else
For i = LBound(attr) To UBound(attr)
objChild.setAttribute attr(i), val(i)
Next
End If
Else
objChild.setAttribute attr, val
End If
objDoc.documentElement.appendChild(objChild)
blnResult = saveFile(strXMLFile)
If blnResult Then
updateRootNodeWAttr = True
End If
Set objChild = Nothing
End Function
Public Function SetAttribute(strAttrName, strAttrValue, objNode)
Dim objAttr ' As MSXML2.IXMLDOMNode
If Not objNode Is Nothing Then
If strAttrValue <> "" or AllowEmptyAttr Then
Set objAttr = objNode.Attributes.getNamedItem(strAttrName)
If objAttr Is Nothing Then
Set objAttr = CreateAttribute(strAttrName, strAttrValue, objNode)
End If
objAttr.Text = strAttrValue
Else
RemoveAttribute strAttrName, objNode
End If
End If
End Function
Public Function addNodeAttribute(strNode, attr, val)
Dim objNode
Dim objAttr
Dim i
Set objNode = objDoc.documentElement.selectSingleNode(strNode)
If IsArray(attr) And IsArray(val) Then
If UBound(attr)-LBound(attr) <> UBound(val)-LBound(val) Then
Exit Function
Else
For i = LBound(attr) To UBound(attr)
Set objAttr = objDoc.createAttribute(attr(i))
objNode.setAttribute attr(i), val(i)
Next
End If
Else
Set objAttr = objDoc.createAttribute(attr)
objNode.setAttribute attr, val
End If
objDoc.Save strFile
Set objAttr = Nothing
End Function
'// Create a child node under the specified XPath Node
Public Function createChildNode(ByVal strXPath, ByVal strNode)
Dim objParent
Dim objChild
createChildNode = False
For Each objParent In objDoc.documentElement.selectNodes(strXPath)
Set objChild = objDoc.createNode(1, strNode, "")
objParent.appendChild(objChild)
Next
blnResult = saveFile(strXMLFile)
If blnResult Then
createChildNode = True
End If
Set objChild = Nothing
Set objParent = Nothing
End Function
'// Create a child node(s) under the specified XPath Node with attributes
Public Function createChildNodeWAttr(ByVal strXPath, ByVal strNode, ByVal attr, ByVal val)
Dim objParent
Dim objChild
Dim objAttr
Dim i
createChildNodeWAttr = False
For Each objParent In objDoc.documentElement.selectNodes(strXPath)
Set objChild = objDoc.createNode(1, strNode, "")
If IsArray(attr) And IsArray(val) Then
If UBound(attr)-LBound(attr) <> UBound(val)-LBound(val) Then
Exit Function
Else
For i = LBound(attr) To UBound(attr)
Set objAttr = objDoc.createAttribute(attr(i))
objChild.SetAttribute attr(i), val(i)
Next
End If
Else
Set objAttr = objDoc.createAttribute(attr)
objChild.setAttribute attr, val
End If
objParent.appendChild(objChild)
Next
blnResult = saveFile(strXMLFile)
If blnResult Then
createChildNodeWAttr = True
End If
Set objChild = Nothing
Set objParent = Nothing
End Function
'// Update a child node(s) under the specified XPath Node with attributes
Public Function updateChildNodeWAttr(ByVal strXPath, ByVal strNode, ByVal attr, ByVal val)
Dim objParent
Dim objChild
Dim objAttr
Dim i
updateChildNodeWAttr = False
For Each objParent In objDoc.documentElement.selectNodes(strXPath)
Set objChild = objDoc.selectSingleNode(strNode)
If IsArray(attr) And IsArray(val) Then
If UBound(attr)-LBound(attr) <> UBound(val)-LBound(val) Then
Exit Function
Else
For i = LBound(attr) To UBound(attr)
objChild.SetAttribute attr(i), val(i)
Next
End If
Else
objChild.setAttribute attr, val
End If
objParent.appendChild(objChild)
Next
blnResult = saveFile(strXMLFile)
If blnResult Then
updateChildNodeWAttr = True
End If
Set objChild = Nothing
Set objParent = Nothing
End Function
Public Function updateNodeWAttr(ByVal strNode, ByVal attr, ByVal val)
Dim objNode
Dim objAttr
Dim i
updateNodeWAttr = False
Set objNode = objDoc.selectSingleNode(strNode)
If IsArray(attr) And IsArray(val) Then
If UBound(attr)-LBound(attr) <> UBound(val)-LBound(val) Then
Exit Function
Else
For i = LBound(attr) To UBound(attr)
objNode.SetAttribute attr(i), val(i)
Next
End If
Else
objNode.setAttribute attr, val
End If
blnResult = saveFile(strXMLFile)
If blnResult Then
updateNodeWAttr = True
End If
Set objNode = Nothing
End Function
'// Delete the node specified by the XPath
Public Function deleteNode(ByVal strXPath)
Dim objNode
deleteNode = False
For Each objNode In objDoc.documentElement.selectNodes(strXPath)
objDoc.documentElement.removeChild objNode
Next
blnResult = saveFile(strXMLFile)
If blnResult Then
deleteNode = True
End If
Set objNode = Nothing
End Function
'// Save the file
Private Function saveFile(ByVal strFile)
saveFile = False
On Error Resume Next
objDoc.Save strFile
If Err.Number <> 0 Then
strMsg = "Error " & Err.Number & " occured." & vbCRLF
strMsg = strMsg & Err.Description & vbCRLF
strMsg = strMsg & "Unable to save file '" & strFile & "'"
WScript.Echo strMsg
End If
On Error Goto 0
saveFile = True
End Function
]]>
</script>
</component>
BTW, if you're using Wise Package Studio, it can handle XML files. After adding an XML file, double-clicking it brings up the 'Dynamic Content' dialog. Search the WPS Help for 'XML' then see the topic 'Editing XML files During Installation'.
Lastly, I can't quite escape the feeling that this post really ought to have been in the 'Scripting' forum....LOL
Posted by:
Inabus
15 years ago
Posted by:
anonymous_9363
15 years ago
Posted by:
Inabus
15 years ago
Posted by:
rayz_0020
15 years ago
Posted by:
anonymous_9363
15 years ago
Ian, I wud definitely look into wat u have suggested.Folks, can I once again make a plea to please try and avoid abbreviation (other than well-used and well-understood ones) and/or text-speak here? For many visitors, English isn't their first language and the practise makes posts that much harder to understand.
Posted by:
Inabus
15 years ago
Posted by:
Inabus
15 years ago
Posted by:
rayz_0020
15 years ago
Folks, can I once again make a plea to please try and avoid abbreviation (other than well-used and well-understood ones) and/or text-speak here? For many visitors, English isn't their first language and the practice makes posts that much harder to understand.
Hmmm... got used to it so badly that, I even draft my official mails in the same style..[:'(]
Its the Spell Check that saves me.... [:D]
Thanks and im glad it helped, my normal day rate of £300 applies ;)
LOL.. No big deal.. Will bill my client with this one too.... If he is SMART enough to pay, we are all millionaires soon....[:D][:D][:D]
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
so that the conversation will remain readable.