Jump to content

Any simple way to get a SINGLE value from XML file?


Go to solution Solved by funkey,

Recommended Posts

Okay, I'm still a relative newbie, and I just spent about 18 hours learning and coding up and debugging my very first AutoIt GUI from scratch (with two simultaneously active windows, too)! :sweating:

So I hope you'll understand that I'm too fatigued to try and learn the particularly complex subject of the XML DOM wrapper UDF and all the associated processing in AutoIt just now. :   Especially since all I need (and all I may ever need) is to obtain just a single value ("<SaveMovieTo>") from a local XML file!  Here are the contents of the XML file in question:

<?xml version="1.0" encoding="utf-8"?>
<Configuration>
    <MajorVersion>1</MajorVersion>
    <SubVersion>1</SubVersion>
    <General>
        <Language>0x0409</Language>
        <AppCurrentMode>0x0001</AppCurrentMode>
        <RunAtStartup>0</RunAtStartup>
        <SaveMovieTo>X:\Storage Folder</SaveMovieTo>
        <Transparency>0.855000</Transparency>
        <Profile>
        </Profile>
        <Skin>std</Skin>
        <Device>
        </Device>
        <StandbyOSD>0</StandbyOSD>
        <CaptureOSD>1</CaptureOSD>
        <SnapshotOSD>1</SnapshotOSD>
        <DiskOSD>1</DiskOSD>
        <StreamOSD>1</StreamOSD>
        <MicOSD>1</MicOSD>
        <TimeShiftMode>0</TimeShiftMode>
        <PreviewMute>0</PreviewMute>
    </General>
    <Stream>
        <ActiveService>
        </ActiveService>
    </Stream>
</Configuration>

I tried -- I really tried -- to read about the XML DOM wrapper UDF and how to use it, but I go so lost and confused that it looks like it would take a week or two to comprehend.  Would anyone be so kind as to just show me the code to get the "<SaveMovieTo>" value?  I'd very much appreciate it!

Link to comment
Share on other sites

check these out:

FileRead()
StringSplit()
_StringBetween()

the first one will read your file to a string; any of the other two will help you figure out what's in between the XML tags you are interested in.

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

  • Solution

Global $sXML = '<?xml version=" 1.0" encoding=" utf - 8"?>' & @CRLF & _
        "<Configuration>" & @CRLF & _
        "    <MajorVersion>1</MajorVersion>" & @CRLF & _
        "    <SubVersion>1</SubVersion>" & @CRLF & _
        "    <General>" & @CRLF & _
        "        <Language>0x0409</Language>" & @CRLF & _
        "        <AppCurrentMode>0x0001</AppCurrentMode>" & @CRLF & _
        "        <RunAtStartup>0</RunAtStartup>" & @CRLF & _
        "        <SaveMovieTo>X:\Storage Folder</SaveMovieTo>" & @CRLF & _
        "        <Transparency>0.855000</Transparency>" & @CRLF & _
        "        <Profile>" & @CRLF & _
        "        </Profile>" & @CRLF & _
        "        <Skin>std</Skin>" & @CRLF & _
        "        <Device>" & @CRLF & _
        "        </Device>" & @CRLF & _
        "        <StandbyOSD>0</StandbyOSD>" & @CRLF & _
        "        <CaptureOSD>1</CaptureOSD>" & @CRLF & _
        "        <SnapshotOSD>1</SnapshotOSD>" & @CRLF & _
        "        <DiskOSD>1</DiskOSD>" & @CRLF & _
        "        <StreamOSD>1</StreamOSD>" & @CRLF & _
        "        <MicOSD>1</MicOSD>" & @CRLF & _
        "        <TimeShiftMode>0</TimeShiftMode>" & @CRLF & _
        "        <PreviewMute>0</PreviewMute>" & @CRLF & _
        "    </General>" & @CRLF & _
        "    <Stream>" & @CRLF & _
        "        <ActiveService>" & @CRLF & _
        "        </ActiveService>" & @CRLF & _
        "    </Stream>" & @CRLF & _
        "</Configuration>"


Global $Value = _XML_GetElementContent($sXML, "Configuration.General.SaveMovieTo")
ConsoleWrite("Error: " & @error & " - Value: " & $Value & @LF)


Func _XML_GetElementContent($sXML, $sPath)
    ;funkey 2014.06.25
    Local $sTemp, $aTemp
    Local $aPath = StringSplit($sPath, ".")
    $sTemp = $sXML

    For $i = 1 To $aPath[0]
        $aTemp = StringRegExp($sTemp, "(?s)(?i)<" & $aPath[$i] & "\b(.*?)</" & $aPath[$i] & ">", 3)
        If @error Then Return SetError($i, 0, "")
        $sTemp = StringStripWS(StringTrimLeft($aTemp[0], StringInStr($aTemp[0], ">")), 3)
    Next

    Return $sTemp
EndFunc   ;==>_XML_GetElementContent

Edit: Updated code to find XML elements with attributes as well.

Edited by funkey

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

In this case (where SaveMovieTo appears just one time), you can simply extract the value with a _StringBetween() or a RegExp.

Local $SaveMovieTo = StringRegExpReplace($sXML, "(?is).+<SaveMovieTo>(.*?)</SaveMovieTo>.+", "$1")
ConsoleWrite($SaveMovieTo)
Link to comment
Share on other sites

XML.Dom:

Global $sXML = '<?xml version="1.0" encoding="utf-8"?>' & @CRLF & _
        "<Configuration>" & @CRLF & _
        "    <MajorVersion>1</MajorVersion>" & @CRLF & _
        "    <SubVersion>1</SubVersion>" & @CRLF & _
        "    <General>" & @CRLF & _
        "        <Language>0x0409</Language>" & @CRLF & _
        "        <AppCurrentMode>0x0001</AppCurrentMode>" & @CRLF & _
        "        <RunAtStartup>0</RunAtStartup>" & @CRLF & _
        "        <SaveMovieTo>X:\Storage Folder</SaveMovieTo>" & @CRLF & _
        "        <Transparency>0.855000</Transparency>" & @CRLF & _
        "        <Profile>" & @CRLF & _
        "        </Profile>" & @CRLF & _
        "        <Skin>std</Skin>" & @CRLF & _
        "        <Device>" & @CRLF & _
        "        </Device>" & @CRLF & _
        "        <StandbyOSD>0</StandbyOSD>" & @CRLF & _
        "        <CaptureOSD>1</CaptureOSD>" & @CRLF & _
        "        <SnapshotOSD>1</SnapshotOSD>" & @CRLF & _
        "        <DiskOSD>1</DiskOSD>" & @CRLF & _
        "        <StreamOSD>1</StreamOSD>" & @CRLF & _
        "        <MicOSD>1</MicOSD>" & @CRLF & _
        "        <TimeShiftMode>0</TimeShiftMode>" & @CRLF & _
        "        <PreviewMute>0</PreviewMute>" & @CRLF & _
        "    </General>" & @CRLF & _
        "    <Stream>" & @CRLF & _
        "        <ActiveService>" & @CRLF & _
        "        </ActiveService>" & @CRLF & _
        "    </Stream>" & @CRLF & _
        "</Configuration>"

$oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.loadXML($sXML)
$oSaveMovieTo = $oXML.SelectSingleNode("//General/SaveMovieTo")
ConsoleWrite($oSaveMovieTo.text & @CRLF)
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.
Link to comment
Share on other sites

Something simple ...  :)

Global $sXML = '<?xml version=" 1.0" encoding=" utf - 8"?>' & @CRLF & _
        "<Configuration>" & @CRLF & _
        "    <MajorVersion>1</MajorVersion>" & @CRLF & _
        "    <SubVersion>1</SubVersion>" & @CRLF & _
        "    <General>" & @CRLF & _
        "        <Language>0x0409</Language>" & @CRLF & _
        "        <AppCurrentMode>0x0001</AppCurrentMode>" & @CRLF & _
        "        <RunAtStartup>0</RunAtStartup>" & @CRLF & _
        "        <SaveMovieTo>X:\Storage Folder</SaveMovieTo>" & @CRLF & _
        "        <Transparency>0.855000</Transparency>" & @CRLF & _
        "        <Profile>" & @CRLF & _
        "        </Profile>" & @CRLF & _
        "        <Skin>std</Skin>" & @CRLF & _
        "        <Device>" & @CRLF & _
        "        </Device>" & @CRLF & _
        "        <StandbyOSD>0</StandbyOSD>" & @CRLF & _
        "        <CaptureOSD>1</CaptureOSD>" & @CRLF & _
        "        <SnapshotOSD>1</SnapshotOSD>" & @CRLF & _
        "        <DiskOSD>1</DiskOSD>" & @CRLF & _
        "        <StreamOSD>1</StreamOSD>" & @CRLF & _
        "        <MicOSD>1</MicOSD>" & @CRLF & _
        "        <TimeShiftMode>0</TimeShiftMode>" & @CRLF & _
        "        <PreviewMute>0</PreviewMute>" & @CRLF & _
        "    </General>" & @CRLF & _
        "    <Stream>" & @CRLF & _
        "        <ActiveService>" & @CRLF & _
        "        </ActiveService>" & @CRLF & _
        "    </Stream>" & @CRLF & _
        "</Configuration>"

Msgbox(0,"", StringRegExp($sXML, '(?is)SaveMovieTo>([^<]+)', 3)[0] )
Link to comment
Share on other sites

Wow, funkey, that's not only absolutely perfect for my needs, it's gorgeous! :thumbsup:

Please accept my deep thanks for your excellent work!  (Naturally, I will give full you full credit of authorship)

I love this community! :bye:

Global $sXML = '<?xml version=" 1.0" encoding=" utf - 8"?>' & @CRLF & _
        "<Configuration>" & @CRLF & _
        "    <MajorVersion>1</MajorVersion>" & @CRLF & _
        "    <SubVersion>1</SubVersion>" & @CRLF & _
        "    <General>" & @CRLF & _
        "        <Language>0x0409</Language>" & @CRLF & _
        "        <AppCurrentMode>0x0001</AppCurrentMode>" & @CRLF & _
        "        <RunAtStartup>0</RunAtStartup>" & @CRLF & _
        "        <SaveMovieTo>X:\Storage Folder</SaveMovieTo>" & @CRLF & _
        "        <Transparency>0.855000</Transparency>" & @CRLF & _
        "        <Profile>" & @CRLF & _
        "        </Profile>" & @CRLF & _
        "        <Skin>std</Skin>" & @CRLF & _
        "        <Device>" & @CRLF & _
        "        </Device>" & @CRLF & _
        "        <StandbyOSD>0</StandbyOSD>" & @CRLF & _
        "        <CaptureOSD>1</CaptureOSD>" & @CRLF & _
        "        <SnapshotOSD>1</SnapshotOSD>" & @CRLF & _
        "        <DiskOSD>1</DiskOSD>" & @CRLF & _
        "        <StreamOSD>1</StreamOSD>" & @CRLF & _
        "        <MicOSD>1</MicOSD>" & @CRLF & _
        "        <TimeShiftMode>0</TimeShiftMode>" & @CRLF & _
        "        <PreviewMute>0</PreviewMute>" & @CRLF & _
        "    </General>" & @CRLF & _
        "    <Stream>" & @CRLF & _
        "        <ActiveService>" & @CRLF & _
        "        </ActiveService>" & @CRLF & _
        "    </Stream>" & @CRLF & _
        "</Configuration>"


Global $Value = _XML_GetElementContent($sXML, "Configuration.General.SaveMovieTo")
ConsoleWrite("Error: " & @error & " - Value: " & $Value & @LF)


Func _XML_GetElementContent($sXML, $sPath)
    ;funkey 2014.06.25
    Local $sTemp, $aTemp
    Local $aPath = StringSplit($sPath, ".")
    $sTemp = $sXML

    For $i = 1 To $aPath[0]
        $aTemp = StringRegExp($sTemp, "(?s)(?i)<" & $aPath[$i] & "\b(.*?)</" & $aPath[$i] & ">", 3)
        If @error Then Return SetError($i, 0, "")
        $sTemp = StringStripWS(StringTrimLeft($aTemp[0], StringInStr($aTemp[0], ">")), 3)
    Next

    Return $sTemp
EndFunc   ;==>_XML_GetElementContent

Edit: Updated code to find XML elements with attributes as well.

Link to comment
Share on other sites

Thanks, orbs.  I was thinking of doing something basic like that, but I wanted to wait to see other options...

check these out:

FileRead()
StringSplit()
_StringBetween()

the first one will read your file to a string; any of the other two will help you figure out what's in between the XML tags you are interested in.

Link to comment
Share on other sites

Thanks to you also, Christian.  I couldn't be sure the XML file would always look the same, though...

It is my way.

very simple, maybe somone else have another more simple.

#include <Process.au3>
#include <Constants.au3>
#include <String.au3>
#include <Array.au3>



  ; Create a constant variable in Local scope of the filepath.
    Local Const $sFile = "C:\savemovie\savemovie.xml"

    ;if you know the line and never change it´s more simple
    $sLine=FileReadLine($sFile, 9)
    $sSearch=_StringBetween($sLine,"<SaveMovieTo>","</SaveMovieTo>")
    $sResult=_ArrayToString($sSearch,"")
    MsgBox(0,"",$sResult)

maybe this one help you.

or it´s the same with FileRead to read all the file instead a single line.

; Create a constant variable in Local scope of the filepath.
    Local Const $sFile = "C:\savemovie\savemovie.xml"

    ;Read all the file 
    $sLine=FileRead($sFile)
    $sSearch=_StringBetween($sLine,"<SaveMovieTo>","</SaveMovieTo>")
    $sResult=_ArrayToString($sSearch,"")
    MsgBox(0,"",$sResult)

Christian Boniakowski.

Link to comment
Share on other sites

I'm very grateful to you, jdelaney, for your contribution! Your gift gives me a good leg up on learning the powerful (if complex) XML Dom UDF...

 

XML.Dom:

Global $sXML = '<?xml version="1.0" encoding="utf-8"?>' & @CRLF & _
        "<Configuration>" & @CRLF & _
        "    <MajorVersion>1</MajorVersion>" & @CRLF & _
        "    <SubVersion>1</SubVersion>" & @CRLF & _
        "    <General>" & @CRLF & _
        "        <Language>0x0409</Language>" & @CRLF & _
        "        <AppCurrentMode>0x0001</AppCurrentMode>" & @CRLF & _
        "        <RunAtStartup>0</RunAtStartup>" & @CRLF & _
        "        <SaveMovieTo>X:\Storage Folder</SaveMovieTo>" & @CRLF & _
        "        <Transparency>0.855000</Transparency>" & @CRLF & _
        "        <Profile>" & @CRLF & _
        "        </Profile>" & @CRLF & _
        "        <Skin>std</Skin>" & @CRLF & _
        "        <Device>" & @CRLF & _
        "        </Device>" & @CRLF & _
        "        <StandbyOSD>0</StandbyOSD>" & @CRLF & _
        "        <CaptureOSD>1</CaptureOSD>" & @CRLF & _
        "        <SnapshotOSD>1</SnapshotOSD>" & @CRLF & _
        "        <DiskOSD>1</DiskOSD>" & @CRLF & _
        "        <StreamOSD>1</StreamOSD>" & @CRLF & _
        "        <MicOSD>1</MicOSD>" & @CRLF & _
        "        <TimeShiftMode>0</TimeShiftMode>" & @CRLF & _
        "        <PreviewMute>0</PreviewMute>" & @CRLF & _
        "    </General>" & @CRLF & _
        "    <Stream>" & @CRLF & _
        "        <ActiveService>" & @CRLF & _
        "        </ActiveService>" & @CRLF & _
        "    </Stream>" & @CRLF & _
        "</Configuration>"

$oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.loadXML($sXML)
$oSaveMovieTo = $oXML.SelectSingleNode("//General/SaveMovieTo")
ConsoleWrite($oSaveMovieTo.text & @CRLF)
Link to comment
Share on other sites

Thanks to you also, mikell!

 

Something simple ...  :)

Global $sXML = '<?xml version=" 1.0" encoding=" utf - 8"?>' & @CRLF & _
        "<Configuration>" & @CRLF & _
        "    <MajorVersion>1</MajorVersion>" & @CRLF & _
        "    <SubVersion>1</SubVersion>" & @CRLF & _
        "    <General>" & @CRLF & _
        "        <Language>0x0409</Language>" & @CRLF & _
        "        <AppCurrentMode>0x0001</AppCurrentMode>" & @CRLF & _
        "        <RunAtStartup>0</RunAtStartup>" & @CRLF & _
        "        <SaveMovieTo>X:\Storage Folder</SaveMovieTo>" & @CRLF & _
        "        <Transparency>0.855000</Transparency>" & @CRLF & _
        "        <Profile>" & @CRLF & _
        "        </Profile>" & @CRLF & _
        "        <Skin>std</Skin>" & @CRLF & _
        "        <Device>" & @CRLF & _
        "        </Device>" & @CRLF & _
        "        <StandbyOSD>0</StandbyOSD>" & @CRLF & _
        "        <CaptureOSD>1</CaptureOSD>" & @CRLF & _
        "        <SnapshotOSD>1</SnapshotOSD>" & @CRLF & _
        "        <DiskOSD>1</DiskOSD>" & @CRLF & _
        "        <StreamOSD>1</StreamOSD>" & @CRLF & _
        "        <MicOSD>1</MicOSD>" & @CRLF & _
        "        <TimeShiftMode>0</TimeShiftMode>" & @CRLF & _
        "        <PreviewMute>0</PreviewMute>" & @CRLF & _
        "    </General>" & @CRLF & _
        "    <Stream>" & @CRLF & _
        "        <ActiveService>" & @CRLF & _
        "        </ActiveService>" & @CRLF & _
        "    </Stream>" & @CRLF & _
        "</Configuration>"

Msgbox(0,"", StringRegExp($sXML, '(?is)SaveMovieTo>([^<]+)', 3)[0] )
Link to comment
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
 Share

×
×
  • Create New...