Sign in to follow this  
Followers 0
Mecano

Read XML file and replace value

9 posts in this topic

#1 ·  Posted (edited)

Hallo AutoIt lovers,

After searching this forum and reading several messages,
I composed the following code but is this good?
(it works but I want some approval from experts.)
 

_shortcut()

Func _shortcut()
    Local $OutlookSettings = @AppDataDir & "\Microsoft\Outlook\Outlook.xml"
    If FileExists($OutlookSettings) Then
        Local $Readonly = StringInStr(FileGetAttrib($OutlookSettings), "R")
        Local $File = FileOpen($OutlookSettings, 0)
        If $File <> -1 Then
            Local $OutlookXML = FileRead($File)
            FileClose($File)
            If StringRegExp($OutlookXML, "(?i)<shortcutInit>0</shortcutInit>") Then
                $OutlookXML = StringReplace($OutlookXML, '<shortcutInit>0</shortcutInit>', _
                        '<shortcutInit>1</shortcutInit>')
                If $Readonly Then FileSetAttrib($OutlookSettings, "-R")
                Local $hFile = FileOpen($OutlookSettings, 130) ; Open file for writing in unicode UTF8 mode
                FileWrite($hFile, $OutlookXML)
                FileClose($hFile)
                If $Readonly Then FileSetAttrib($OutlookSettings, "+R") ; keep readonly
            EndIf
        EndIf
    Else
        MsgBox(48, "Outlook.xml", "Outlook.xml not found!", 3)
        Exit
    EndIf
EndFunc   ;==>_shortcut

part of Outlook.xml :

<?xml version="1.0"?>
<wundbar>
    <version>124518</version>
    <mailInit>1</mailInit>
    <shortcutInit>1</shortcutInit>
    <initMail>1</initMail>
    <initShortcuts>1</initShortcuts>
    <dataversion>1202</dataversion>
    <stores>

Thanks in advance

Edited by Mecano

Share this post


Link to post
Share on other sites



As long as you do not have multiple entities with the same parameter and value then this code is fine. However if you will have to parse more complex XMLs I'd suggest using XML UDF, that you can find here: '?do=embed' frameborder='0' data-embedContent>>

Share this post


Link to post
Share on other sites

Thanks for the answer and pointing to XML UDF,
 
My concern was about StringRegExp ( pattern pcrepattern ) and   FileOpen($OutlookSettings, 130) ; Open file for writing in unicode UTF8 mode

Share this post


Link to post
Share on other sites

One other question, if I want to remove the line <shortcutInit>0</shortcutInit> or <shortcutInit>1</shortcutInit> whats the correct pattern?
 
This will leave a empty line

  If StringRegExp($OutlookXML, "(?i)<shortcutInit>0</shortcutInit>") Then
                $OutlookXML = StringReplace($OutlookXML, '<shortcutInit>0</shortcutInit>', '')

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

$OutlookXML = StringRegExpReplace($OutlookXML, '.*<shortcutInit>\d</shortcutInit>.*\R', "")

You have to remove also the leading/trailing space(s) and the newline sequence  :)

Edited by mikell

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

XML dom will be soooo much easier.  You can always brute force a regexp to work, but why do that when there are com objects specifically for your question?

Example


 

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

@jdelaney thanx, but see mine openings question.

@mikell excellent solution, works perfect :thumbsup:

never thought of StringRegExpReplace

For non-digits can i use this, example: <textversion>False</textversion>

$OutlookXML = StringRegExpReplace($OutlookXML, '.*<textversion>(.*)</textversion>.*\R', "")
Edited by Mecano

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Hallo AutoIt lovers,

After searching this forum and reading several messages,

I composed the following code but is this good?

 

I would suggest this is not a 'good' route, and you should use a tool that's more suited for it.

I would also suggest, that this is a much better route:

Local $oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.load("test.xml")
$oShortcutInit = $oXML.SelectSingleNode("//shortcutInit")
$oShortcutInit.text = Int($oShortcutInit.text) + 1
$oXML.save("test.xml")
Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

@jdelaney, gonna give it a try, time to learn AutoIt/Microsoft.XMLDOM

Thanks for reply

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0