Based on the tip you gave me, here is the final script which is currently chowing through an xml with over 1000 entries (writing out a lot of little XMLs, just as I wanted).
global target_file
global theXML
global thetitle
global theMusic
tell application "System Events"
set thefile to (choose file)
set x to the POSIX path of thefile
set f to the XML file x
set root to XML element 1 of f
set myRecords to every XML element of root whose name = "RECORD"
end tell
repeat with NextRoot in myRecords
set theMusic to ""
tell application "System Events"
get value of every XML element of NextRoot
set theHost to the value of XML element 2 of NextRoot
set theSynopsis to the value of XML element 6 of NextRoot
set theMusic to the value of XML element 3 of NextRoot as string
if theMusic is missing value then set theMusic to ""
--Had to put this in because there were a few records that didn't have Music performance
set thetitle to the value of XML element 1 of NextRoot
end tell
set theXML to ("<?xml version=\"1.0\"?>
<FinalCutServer>
<entity entityType=\"asset\" entityId=\"/asset/" & thetitle & "\">
<metadata>
<mdValue fieldName=\"Host\" dataType=\"string\">" & theHost & "</mdValue>
<mdValue fieldName=\"Musical Guest\" dataType=\"string\">" & theMusic & "</mdValue>
<mdValue fieldName=\"Synopsis\" dataType=\"string\">" & theSynopsis & "</mdValue>
</metadata>
</entity>
</FinalCutServer>")
--set thetargetpath to (path to desktop)
set target_file to ((path to desktop) & "ParseTest:" & thetitle & ".xml")
--this next part isn't needed, unless you want a copy in the trash
--try
-- set theTarget to (target_file as alias)
-- if theTarget exists then
-- tell application "Finder"
-- delete theTarget
-- end tell
-- end if
--end try
write_to_file(theXML)
end repeat
end run
on write_to_file(theXML)
set the target_file to the target_file as text
--better error trapping for open/write/close
try
set the open_target_file to ¬
open for access file target_file with write permission
on error
close access file target_file
set the open_target_file to ¬
open for access file target_file with write permission
end try
set eof of open_target_file to 0
write theXML to the open_target_file starting at eof
close access the open_target_file
end write_to_file
On May 26, 2008, at 6:53 PM, Ed Stockly wrote:
On May 26, 2008, at 5:11 PM, Anton Linecker wrote:
Hello all,
I am working on parsing some XML with Applescript:
What I have so far is this:
Try this:
global target_file
global theXML
global thetitle
tell application "System Events"
set thefile to (choose file)
set x to the POSIX path of thefile
set f to the XML file x
set root to XML element 1 of f
set myRecords to every XML element of root whose name = "RECORD"
end tell
repeat with NextRoot in myRecords
tell application "System Events" get value of every XML element of NextRoot
set theHost to the value of XML element 2 of NextRoot
set theSynopsis to the value of XML element 6 of NextRoot
set theMusic to the value of XML element 3 of NextRoot
set thetitle to the value of XML element 1 of NextRoot
end tell
set theXML to ("<?xml version=\"1.0\"?>
<FinalCutServer>
<entity entityType=\"asset\" entityId=\"/asset/" & thetitle & "\">
<metadata>
<mdValue fieldName=\"Host\" dataType=\"string\">" & theHost & "</mdValue>
<mdValue fieldName=\"Musical Guest\" dataType=\"string\">" & theMusic & "</mdValue>
<mdValue fieldName=\"Synopsis\" dataType=\"string\">" & theSynopsis & "</mdValue>
</metadata>
</entity>
</FinalCutServer>")
set target_file to ((path to desktop) & thetitle & ".xml")
--this next part isn't needed, unless you want a copy in the trash
--try
-- set theTarget to (target_file as alias)
-- if theTarget exists then
-- tell application "Finder"
-- delete theTarget
-- end tell
-- end if
--end try
write_to_file(theXML)
end repeat
end run
on write_to_file(theXML)
set the target_file to the target_file as text
--better error trapping for open/write/close
try
set the open_target_file to ¬
open for access file target_file with write permission
on error
close access file target_file
set the open_target_file to ¬
open for access file target_file with write permission
end try
set eof of open_target_file to 0
write theXML to the open_target_file starting at eof
close access the open_target_file
end write_to_file