Re: Working with XML
Re: Working with XML
- Subject: Re: Working with XML
- From: Ed Stockly <email@hidden>
- Date: Mon, 26 May 2008 18:53:00 -0700
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
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden