parsing an XML file
parsing an XML file
- Subject: parsing an XML file
- From: email@hidden
- Date: Wed, 4 May 2005 00:19:01 +0100
Hi from a newbie to AppleScript,
I am trying to parse a large (200k) XML file to extract a few bits of data.
I have tried a variety of methods including pure AppleScript, Java XML and XSL templates and, most recently, Satimage's XMLLib osax (as an AppleScript plug-in). With each one, I have got so far but then get stuck!
A cut-down version of the XML is:
<Project>
<Movie>
<Movie_XML_File>
<Stuff_in_here></Stuff_in_here>
</Movie_XML_File>
<Track>
<Stuff_in_here></Stuff_in_here>
<Track_Name>"First"</Track_Name>
</Track>
<Track>
<Stuff_in_here></Stuff_in_here>
<Track_Name>"Second"</Track_Name>
</Track>
<Track>
<Track_XML_File>
<Stuff_in_here></Stuff_in_here>
</Track_XML_File>
<Picture_Sample>
<Picture_Sample_File>
<Stuff_in_here></Stuff_in_here>
<Media_File_Spec><VRefNum>-101</VRefNum><ParID>3709</ParID><FileName>"image1.jpg"</FileName></Media_File_Spec>
</Picture_Sample_File>
<Stuff_in_here></Stuff_in_here>
<Sample_Duration><Time>12000</Time><TimeScale>600</TimeScale></Sample_Duration>
<Sample_Start_Time><Time>19800</Time><TimeScale>600</TimeScale></Sample_Start_Time>
<Image_Codec>"raw "</Image_Codec>
<Image_Quality>"best"</Image_Quality>
</Picture_Sample>
<Picture_Sample>
<Picture_Sample_File>
<Stuff_in_here></Stuff_in_here>
<Media_File_Spec><VRefNum>-101</VRefNum><ParID>3709</ParID><FileName>"image2.jpg"</FileName></Media_File_Spec>
</Picture_Sample_File>
<Stuff_in_here></Stuff_in_here>
<Sample_Duration><Time>16800</Time><TimeScale>600</TimeScale></Sample_Duration>
<Sample_Start_Time><Time>60000</Time><TimeScale>600</TimeScale></Sample_Start_Time>
<Image_Codec>"raw "</Image_Codec>
<Image_Quality>"best"</Image_Quality>
</Picture_Sample>
<Track_Type>"PICt"</Track_Type>
<Window_Scale>0</Window_Scale>
<Track_ID>4171</Track_ID>
<Track_Name>"Slides "</Track_Name>
<Track_Language>"Engl"</Track_Language>
<Track_Quality>72</Track_Quality>
</Track>
<Track>
<Stuff_in_here></Stuff_in_here>
<Track_Name>"Third"</Track_Name>
</Track>
</Movie>
</Project>
Within this XML file I am trying to locate the single <Track> with a <Track_Name> of "Slides" from the many <Track>'s.
Then, within this <Track> node, locate all the <Picture_Sample> nodes and from each of these, extract the:
<Picture_Sample_File>
<MediaFile_Spec>
<FileName> as a string - e.g. "image2.jpg"
along with this file's:
<Sample_Start_Time>
<Time>
<TimeScale>
and:
<Sample_Duration>
<Time>
<TimeScale>
The output I am hoping to achieve is roughly:
image1.jpg,19800,600,12000,600
image2.jpg,60000,600,16800,600
With AppleScript and XMLLib, I can't seem to programmatically traverse the XML tree.
This code:
set the_file to choose file
set the_doc to XMLOpen the_file
set the_root to XMLRoot the_doc
set {my_track} to XMLXPath the_root with "Movie/Track[Track_Name=\"Slides\"]"
XMLDisplayXML my_track
gives a "Can't get item 1 of {}" error, yet I can get to the right track with:
set the_file to choose file
set the_doc to XMLOpen the_file
set the_root to XMLRoot the_doc
set the_movie to XMLChild the_root index 1
set myTrack to XMLChild the_movie index 4 -- Correct but hard-coded!
XMLDisplayXML myTrack
Can someone nudge me in the right direction?
Many thanks (and sorry for the long post),
Andrew _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden