Overblog Tous les blogs Top blogs Technologie & Science Tous les blogs Technologie & Science
Editer l'article Suivre ce blog Administration + Créer mon blog
MENU

Modify xml file

Publié le par damcuvelier

 

 

dim valuexml,xmlDoc,Element


'Open logFile
Set logFile = fso.OpenTextFile(logFilePath, ForAppending, True)

'// String handling Variables
dim filexml : filexml = "<your xmlfile>"
dim newvalue : newvalue = "<the value your want to write instead of>"
'<section1>
'    <section2>
'        <section3 Action1="0" Action2="0"></section3>
dim sectionxml : sectionxml = "section1/section2/section3"
dim variablexml : variablexml = "Action1"
'here valuexml to change is "0" so newvalue=2 instead of 0

modifxml filexml,sectionxml,variablexml,newvalue


'xml actions
'modify xml:
function modifxml(filexml,sectionxml,variablexml,valuexml)
    Set xmlDoc = CreateObject("Microsoft.XMLDOM")
    xmlDoc.Async = "false"
    xmlDoc.Load(filexml)
'get elements
        For Each Element In xmlDoc.selectNodes(sectionxml)
            Element.setAttribute variablexml,valuexml
        Next

'set new values
    dim rdr : set rdr = CreateObject("MSXML2.SAXXMLReader")
    dim wrt : set wrt = CreateObject("MSXML2.MXXMLWriter")
    dim oStream : Set oStream = CreateObject("ADODB.STREAM")
    oStream.Open
    oStream.Charset = "ISO-8859-1"

    wrt.indent = True
    wrt.encoding = "ISO-8859-1"
    wrt.output = oStream
    Set rdr.contentHandler = wrt
    Set rdr.errorHandler = wrt
    rdr.Parse xmlDoc
    wrt.flush

'save xml file modified
    oStream.SaveToFile filexml, 2

    wscript.echo "Change <" & sectionxml & "> : the value " & variablexml & " to " & chr(34) & valuexml & chr(34) & "."
    Set rdr = Nothing
    Set wrt = Nothing
    Set xmlDoc = Nothing
end function

Commenter cet article