Hi all,

I have to some changes in(current user name has to replace on old user name) XML file, If different user loggs in.
That XML file has to contain currently logged in uaer name. how to do it in vb script.


EX: file Content likeC:\test\test.XML in that C:\users\USER NAME\test..etc...
i need to replace USER NAME with currently logged in user name.
0 Comments   [ + ] Show Comments

Comments

Please log in to comment

Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.

Answers

0
Here's a class file for dealing with XML files. It will allow you to access the file in the proper way, rather than the brain-dead line-by-line parsing which I have seen passed off as XMl handling. You should use one of the 'update' functions. I can't tell you which, as you don't say whether your string is part of an element, an attribute or whatever.

I suggest you use a copy of the file while you experiment with the various functions. '// Dim blnResult
'// Dim objXML
'// Dim strPath
'// Dim str
'//
'// Set objXML = New clsXML
'//
'// strPath = "New.xml"
'//
'// With objXML
'// .createFile strPath, "Root"
'//
'// Or if you want to use an existing XML file:
'// '.File = strPath
'// blnResult = .SetXMLFileName(strPath)
'//
'// .createRootChild "Images"
'//
'// '// Here, only one attribute is added to the Images/Image Node
'// .createChildNodeWAttr "Images", "Image", "id", "1"
'// .updateField "Images//Image[@id=1]", "super.gif"
'// .createRootNodeWAttr "Jobs", Array("Size", "Length", "Width"), Array(24, 31, 30)
'// .createRootNodeWAttr "Jobs", Array("Size", "Length", "Width"), Array(24, 30, 29)
'// .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
'// .updateField "Jobs[@Size=24]", "24's"
'//
'// '// Notice that only two nodes have the specified XPath:
'// '// thus, only two new child nodes will be added
'// .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 .getField("Jobs[@Size=24]")
'// WScript.Echo str & vbCRLF
'// Next
'// End With
'//
'// Also, note this syntax for adding children to multiple parent nodes with the same name:
'//
'// With objXML
'// .openFile(strPath)
'//
'// .createRootNodeWAttr "ASX", "version", "3.0"
'//
'// .createChildNodeWAttr "ASX", "PARAM", Array("NAME", "VALUE"), Array("Encoding", "UTF-8")
'//
'// .createChildNode "ASX", "ENTRY"
'// .createChildNode "ASX", "ENTRY"
'//
'// .createChildNodeWAttr "ASX//ENTRY[0]", "REF", "href", "\Storage Card\Music\ABBA\Gold\Waterloo.mp3"
'// .createChildNodeWAttr "ASX//ENTRY[1]", "REF", "href", "\Storage Card\Music\ABBA\Gold\Chiquitita.mp3"
'// End With
'//
'// Set objXML = Nothing

Class clsXML

Private objDoc
Private strMsg
Private objFSO
Private blnResult

Public strXMLFile
Public NODE_ELEMENT '// The node represents an element (its nodeTypeString property is "element").
'// An Element node can have the following child node types: Element, Text, Comment,
'// ProcessingInstruction, CDATASection, and EntityReference. The Element node can be
'// the child of the Document, DocumentFragment, EntityReference, and Element nodes.

Public NODE_ATTRIBUTE '// The node represents an attribute of an element (its nodeTypeString property is "attribute").
'// An Attribute node can have the following child node types: Text and EntityReference.
'// The Attribute node does not appear as the child node of any other node type;
'// it is not considered a child node of an Element.

Public NODE_TEXT '// The node represents the text content of a tag (its nodeTypeString property is "text").
'// A Text node cannot have any child nodes. The Text node can appear as the child node
'// of the Attribute, DocumentFragment, Element, and EntityReference nodes.

Public NODE_CDATA_SECTION '// The node represents a CDATA section in the XML source (its nodeTypeString property
'// is "cdatasection"). CDATA sections are used to escape blocks of text that would otherwise
'// be recognized as markup. A CDATASection node cannot have any child nodes.
'// The CDATASection node can appear as the child of the DocumentFragment, EntityReference,
'// and Element nodes.

Public NODE_ENTITY_REFERENCE '// The node represents a reference to an entity in the XML document (its nodeTypeString property
'// is "entityreference"). This applies to all entities, including character entity references.
'// An EntityReference node can have the following child node types: Element, ProcessingInstruction,
'// Comment, Text, CDATASection, and EntityReference. The EntityReference node can appear as the
'// child of the Attribute, DocumentFragment, Element, and EntityReference nodes.

Public NODE_ENTITY '// The node represents an expanded entity (its nodeTypeString property is "entity").
'// An Entity node can have child nodes that represent the expanded entity
'// (for example, Text and EntityReference nodes). The Entity node can appear as the child
'// of the DocumentType node.

Public NODE_PROCESSING_INSTRUCTION '// The node represents a processing instruction from the XML document (its nodeTypeString property
'// is "processinginstruction"). A ProcessingInstruction node cannot have any child nodes.
'// The ProcessingInstruction node can appear as the child of the Document, DocumentFragment,
'// Element, and EntityReference nodes.

Public NODE_COMMENT '// The node represents a comment in the XML document (its nodeTypeString property is "comment").
'// A Comment node cannot have any child nodes. The Comment node can appear as the child of the
'// Document, DocumentFragment, Element, and EntityReference nodes.

Public NODE_DOCUMENT '// The node represents a document object, that as the root of the document tree, provides access
'// to the entire XML document (its nodeTypeString property is "document"). It is created using
'// the progID "Microsoft.XMLDOM" or through a data island using <XML> or <SCRIPT LANGUAGE=XML>.
'// A Document node can have the following child node types: Element (maximum of one),
'// ProcessingInstruction, Comment, and DocumentType. The Document node cannot appear as
'// the child of any node types.

Public NODE_DOCUMENT_TYPE '// The node represents the document type declaration, indicated by the <!DOCTYPE> tag
'// (its nodeTypeString property is "documenttype"). A DocumentType node can have the following
'// child node types: Notation and Entity. The DocumentType node can appear as the child of the
'// Document node.

Public NODE_DOCUMENT_FRAGMENT '// The node represents a document fragment (its nodeTypeString property is "documentfragment").
'// The DocumentFragment node associates a node or subtree with a document without actually
'// being contained within the document. A DocumentFragment node can have the following child node
'// types: Element, ProcessingInstruction, Comment, Text, CDATASection, and EntityReference.
'// The DocumentFragment node cannot appear as the child of any node types.

Public NODE_NOTATION '// The node represents a notation in the document type declaration (its nodeTypeString property
'// is "notation"). A Notation node cannot have any child nodes. The Notation node can appear as
'// the child of the DocumentType node.

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 intIndex

Set objNodeList = objDoc.documentElement.selectNodes(strXPath)
ReDim arrResponse(objNodeList.length - 1)

For intIndex = 0 To objNodeList.length - 1
arrResponse(intIndex) = objNodeList.item(intIndex).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(NODE_ELEMENT, strNode, "")
objDoc.documentElement.appendChild(objChild)

blnResult = saveFile(strXMLFile)
If blnResult Then
createRootChild = True
End If

Set objChild = Nothing
End Function

'// Create a root node with attributes
Public Function createRootNodeWAttr(ByVal strNode, ByVal strAttribute, ByVal strValue)
Dim objChild
Dim objAttr
Dim intIndex

createRootNodeWAttr = False

Set objChild = objDoc.createNode(NODE_ELEMENT, strNode, "")
If IsArray(strAttribute) And IsArray(strValue) Then
If UBound(strAttribute)-LBound(strAttribute) <> UBound(strValue)-LBound(strValue) Then
Exit Function
Else
For intIndex = LBound(strAttribute) To UBound(strAttribute)
Set objAttr = objDoc.createAttribute(strAttribute(intIndex))
objChild.setAttribute strAttribute(intIndex), strValue(intIndex)
Next
End If
Else
Set objAttr = objDoc.createAttribute(strAttribute)
objChild.setAttribute strAttribute, strValue
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 strAttribute, ByVal strValue)
Dim objChild
Dim objAttr
Dim intIndex

updateRootNodeWAttr = False

Set objChild = objDoc.selectSingleNode(strNode)
If IsArray(strAttribute) And IsArray(strValue) Then
If UBound(strAttribute)-LBound(strAttribute) <> UBound(strValue)-LBound(strValue) Then
Exit Function
Else
For intIndex = LBound(strAttribute) To UBound(strAttribute)
objChild.setAttribute strAttribute(intIndex), strValue(intIndex)
Next
End If
Else
objChild.setAttribute strAttribute, strValue
End If

objDoc.documentElement.appendChild(objChild)

blnResult = saveFile(strXMLFile)
If blnResult Then
updateRootNodeWAttr = True
End If

Set objChild = 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(NODE_ELEMENT, 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 strAttribute, ByVal strValue)
Dim objParent
Dim objChild
Dim objAttr
Dim intIndex

createChildNodeWAttr = False

For Each objParent In objDoc.documentElement.selectNodes(strXPath)
Set objChild = objDoc.createNode(NODE_ELEMENT, strNode, "")
If IsArray(strAttribute) And IsArray(strValue) Then
If UBound(strAttribute)-LBound(strAttribute) <> UBound(strValue)-LBound(strValue) Then
Exit Function
Else
For intIndex = LBound(strAttribute) To UBound(strAttribute)
Set objAttr = objDoc.createAttribute(strAttribute(intIndex))
objChild.SetAttribute strAttribute(intIndex), strValue(intIndex)
Next
End If
Else
Set objAttr = objDoc.createAttribute(strAttribute)
objChild.setAttribute strAttribute, strValue
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 strAttribute, ByVal strValue)
Dim objParent
Dim objChild
Dim objAttr
Dim intIndex

updateChildNodeWAttr = False

For Each objParent In objDoc.documentElement.selectNodes(strXPath)
Set objChild = objDoc.selectSingleNode(strNode)
If IsArray(strAttribute) And IsArray(strValue) Then
If UBound(strAttribute)-LBound(strAttribute) <> UBound(strValue)-LBound(strValue) Then
Exit Function
Else
For intIndex = LBound(strAttribute) To UBound(strAttribute)
objChild.SetAttribute strAttribute(intIndex), strValue(intIndex)
Next
End If
Else
objChild.setAttribute strAttribute, strValue
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 strAttribute, ByVal strValue)
Dim objNode
Dim objAttr
Dim intIndex

updateNodeWAttr = False

Set objNode = objDoc.selectSingleNode(strNode)
If IsArray(strAttribute) And IsArray(strValue) Then
If UBound(strAttribute)-LBound(strAttribute) <> UBound(strValue)-LBound(strValue) Then
Exit Function
Else
For intIndex = LBound(strAttribute) To UBound(strAttribute)
objNode.SetAttribute strAttribute(intIndex), strValue(intIndex)
Next
End If
Else
objNode.setAttribute strAttribute, strValue
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

Private Sub Class_Initialize()

NODE_ELEMENT = 1
NODE_ATTRIBUTE = 2
NODE_TEXT = 3
NODE_CDATA_SECTION = 4
NODE_ENTITY_REFERENCE = 5
NODE_ENTITY = 6
NODE_PROCESSING_INSTRUCTION = 7
NODE_COMMENT = 8
NODE_DOCUMENT = 9
NODE_DOCUMENT_TYPE = 10
NODE_DOCUMENT_FRAGMENT = 11
NODE_NOTATION = 12
End Sub

Private Sub Class_Terminate()

End Sub

End Class
Answered 08/03/2010 by: VBScab
Red Belt

Please log in to comment
0
Thx

Here is one more easy solution for it who are using installsheild 2010, there is an option called "TEXT FILE CHANGES" you can use this option to do so even @ run time.
Answered 08/05/2010 by: skt
Orange Senior Belt

Please log in to comment
Answer this question or Comment on this question for clarity