Sign in to follow this  
Followers 0
scriptkitty

Example Of Parseing An Xml File

2 posts in this topic

This is just a learning tool to show how to parse information from a XML file to Tab or CSV.

The acme store is the default Yahoostore example.

I only Parsed out a few things on purpose. My caption had HTML, so I did a few replaces.

filedelete("out.tab")
URLDownloadToFile ("http://store.yahoo.com/acme/objinfo.xml","yahoo.xml")

$file = FileOpen("yahoo.xml", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
$test=""
; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
$nospace=StringReplace($line," ","")
 if Stringleft($nospace,12)='<ProductId="' then 
 $trim=Stringtrimleft($line,StringInstr($line,'<ProductId="')+17)
 $IDname=Stringtrimright($trim,2)
; MsgBox(0, "Line read:", $IDname)
 $test=1
endif
if $test=1 and Stringleft($nospace,6)='<code>' then
 $trim=Stringtrimleft($line,StringInstr($line,'<code>')+5)
 $Code=Stringtrimright($trim,7)
; MsgBox(0, "Line read:", $Code)
 $test=2
endif
if $test=2 and Stringleft($nospace,13)='<Description>' then
 $trim=Stringtrimleft($line,StringInstr($line,'<Description>')+12)
 $Name=Stringtrimright($trim,14)
; MsgBox(0, "Line read:", $Name)
 $test=3
endif
if $test=3 and Stringleft($nospace,9)='<Caption>' then
 $Caption=Stringtrimleft($line,StringInstr($line,'<Caption>')+8)
 $Caption=Stringtrimright($Caption,10)
$Caption=Stringreplace($caption,@tab," ")
$Caption=Stringreplace($caption,"&lt;","<")
$Caption=Stringreplace($caption,"&gt;",">")


 $test=4
; MsgBox(0, "Line read:", $caption)
endif

if $test=4 then
$test=""
Writeit($code&@tab&$name&@tab&$IDname&@tab&$caption)
endif

Wend
FileClose($file)
msgbox(1,"Done","",5)


func writeit($out)
FileWriteLine("out.tab",$out)
endfunc

basically XML data looks like this:

<Products>
    <Product Id="10gifcer">
      <Code>GC-10</Code>
      <Description>$10 Gift Certificate</Description>
      <Url>http://store.yahoo.com/acme/10gifcer.html</Url>
      <Thumb>&lt;img border=0 width=70 height=44 src=http://us.st1.yimg.com/store1.yimg.com/I/acme_1781_182775&gt;</Thumb>
      <Picture>&lt;img border=0 width=100 height=63 src=http://us.st1.yimg.com/store1.yimg.com/I/acme_1781_185528&gt;</Picture>
      <Orderable>YES</Orderable>
      <Taxable>NO</Taxable>
      <Pricing>
        <BasePrice>80.00</BasePrice>
        <LocalizedBasePrice>80.00</LocalizedBasePrice>
        <GiftwrapCharge>1.2</GiftwrapCharge>
      </Pricing>
      <Path>
        <ProductRef Id="gifts" Url="http://store.yahoo.com/acme/gifts.html">Gifts</ProductRef>
      </Path>
      <Availability>Usually ships the same business day.</Availability>
    </Product>

AutoIt3, the MACGYVER Pocket Knife for computers.

Share this post


Link to post
Share on other sites



:whistle: Wow, this is very cool. I hope I can understand what you have going on in this script as it would solve a problem I have been fighting with for a while. Thanks for sharing this!

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