Jump to content

simple XML Reader


h3ir
 Share

Recommended Posts

Have a look at this:

It will read a simple XML Structure and Output it as Array and write additional the parsed XML in a Log-File...

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.12.0
 Author:         h3ir

 Script Function:
    Easy XML Parser
    
 XML Example:
<Path>
 <Point>
     <Description>#1</Description>
     <X>120</X>
     <Y>300</Y>
 </Point>
 <Point>
     <Description>#2</Description>
     <X>150</X>
     <Y>250</Y>
 </Point>
</Path>

#ce ----------------------------------------------------------------------------

#include-once

Func __ParseXML($XMLfile)
    
    FileDelete(@ScriptDir & "\parse.log")

    $file = FileOpen($XMLfile, 0)

    $i = 0
    $j = 0

    While 1
        
        $i = $i+1
        $line = FileReadLine($file, $i)
        $line = StringReplace($line, " ", "")
        $line = StringReplace($line, @tab, "")
        
        If $line == "<Point>" Then
            $j = $j+1
        EndIf
        
        If $line == "</Path>" Then
            ExitLoop
        EndIf
        
    WEnd

    Dim $Path[$j][3]

    $Point = 0
    $i = 0
    $j = -1

    While 1
        
        $i = $i+1
        $line = FileReadLine($file, $i)
        $line = StringReplace($line, " ", "")
        $line = StringReplace($line, @tab, "")
        
        If $line == "<Point>" Then
            $Point = 1
            $j = $j+1
        EndIf
        
        If $Point == 1 AND StringLeft($line,13)='<Description>' then
            $trim = StringTrimLeft($line,StringInstr($line,'<Description>')+12)
            $Path[$j][0] = StringTrimRight($trim,14)
        EndIf

        If $Point == 1 AND StringLeft($line,3)='<X>' then
            $trim = StringTrimLeft($line,StringInstr($line,'<X>')+2)
            $Path[$j][1] = StringTrimRight($trim,4)
        EndIf

        If $Point == 1 AND StringLeft($line,3)='<Y>' then
            $trim = StringTrimLeft($line,StringInstr($line,'<Y>')+2)
            $Path[$j][2] = StringTrimRight($trim,4)
        EndIf

        If $Point == 1 AND $line == "</Point>" Then
            $Point = 0
        EndIf
        
        If $Point == 0 AND $line == "</Path>" Then
            ExitLoop
        EndIf

    WEnd

    $maxArray = Ubound($Path)

    $i = 0
    While $i <= $maxArray-1
        $log = FileOpen(@ScriptDir & "\parse.log",1)
        FileWriteLine($log, $Path[$i][0]&@tab&$Path[$i][1]&@tab&$Path[$i][2])
        FileClose($log)
        $i = $i+1
    WEnd

    Return $Path

EndFunc
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...