Parsing XML file

Scripters

I am in the process of adapting Apple’s new item folder action to create a folder based on name and content of new item added.

The first part was determining whether a new folder was needed from the name of the item dropped into the watched folder.

The second part is to parse the XML contents of new item to determine the path and name of the new folder.

Question: should I pass the contents to a Unix script or is there a good way of parsing this sort of thing in AppleScript?

“<?xml version=\"1.0\" encoding=\"utf-8\"?>
<Transaction xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance\” xmlns="http://tempuri.org" xsi:schemaLocation="http://tempuri.org myschema.xsd">
<Project Key="A123" Title="Title’s name has non-alpha characters" Long_Name="So does the long name" Company_Name="ACME & CO">


"

Regards
C.

Well you could use Late Night Software’s XML Scripting Addition or if you only need to pull a piece or two out of it you could use Text Item Delimiters…

Or the shell as you mentioned

On OS X v10.4 or later, you can also use System Event’s XML Suite.

Bruce:

Do you know how to use the terms in the XML suite of System Events in a script? I have been playing around with them, but no luck so far; nor could I locate any threads here (or examples at the Apple web site).

Yes, I find System Events a little difficult also. But for just getting a piece out it’s OK. I saved your data as a file. Then, to get the “Title” attribute:

set xmlFile to (choose file) as Unicode text
tell application “System Events”
set theXML to contents of XML file xmlFile
set theTitle to value of XML attribute “Title” of XML element “Project” of XML element “Transaction” of theXML
end tell

Thanks for the information, Fenton. It appears that in order to use the XML suite of System Events, you need to know in advance how the XML is constructed in the first place. Is that correct? Do you know of any way to simply point it to an XML file and return elements, attributes, values, and tree structures?