Hi,
Sorry for my VERRY VERRY bad english, I am french.
I woud like to make an little application to get data in “Core Data” system (XML file) and put the result in iCal.
I know how to put in iCal, but for XML parsing, … i don’t found.
XML sample:
i need a code to get all “attribute” items with name=“site” into an “object” item with type=“PLONGEES”
You can do this with System Events, although this took me a while to get it all working.
You end up with a big list of small lists of the data. I’ll assume you would know how to repeat through that and get what you want. If not, post back and one of us can help you.
set theFinalValues to {}
set XMLfile to (path to desktop as text) & "TestXML.xml"
tell application "System Events"
tell XML element "database" of contents of XML file XMLfile
repeat with thisElement from 1 to (count of XML elements)
set numberAttributes to count XML attribute of XML element thisElement
set objectAttributesList to {}
set typeValue to ""
set idValue to ""
repeat with cntr from 1 to numberAttributes
if (name of XML attribute cntr of XML element thisElement) is "type" then
set typeValue to (value of XML attribute cntr of XML element thisElement)
end if
if (name of XML attribute cntr of XML element thisElement) is "id" then
set idValue to (value of XML attribute cntr of XML element thisElement)
end if
end repeat
try
set subAttName to (value of XML attribute 1 of XML element 1 of XML element thisElement)
on error
set subAttName to ""
end try
try
set finalValue to value of XML element 1 of XML element thisElement
on error
set finalValue to ""
end try
if typeValue = "PLONGEES" and subAttName = "site" then
set theFinalValues to theFinalValues & {{typeValue, idValue, subAttName, finalValue}}
end if
end repeat
end tell
end tell
theFinalValues
Model: iMac Intel 10.5.8
Browser: Firefox 3.0.2
Operating System: Mac OS X (10.5)
Another approach would be to parse an XML file with an XSL stylesheet file, using the built-in XML processor of OS X. It does not produce individual AppleScript variables, as it is more an “all at once” result, but it can produce just about anything else. It could produce plain text, or xml, or text formatted a specific way which iCal could import. That last output would be tedious to set up, but there are examples around. (I wish these apps, iCal, Address Book, would support xml export/import.)
The XSL to just get a text result, of separate lines (which you could transform to an AppleScript list with “paragraphs of”).
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" version="1.0" encoding="utf-8" />
<xsl:template match="/">
<xsl:for-each select="database/object[@type='PLONGEES']">
<xsl:value-of select="attribute[@name='site']" />
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
[Inside the xsl:text element is supposed to be a URL encoded ASCII 10 line break. The forum parser will not show that.]
The AppleScript. This script is set to process the Plongees.xml file using the Plongees.xsl file, in the same folder as the AppleScript (requires a version of AppleScript, or an applet, where the script knows its own location).
tell application "Finder" to set my_path to container of (path to me) as alias
set my_posix_path to quoted form of POSIX path of my_path
set xml_file to my_posix_path & "Plongees.xml"
set xsl_file to my_posix_path & "Plongees.xsl"
set plongees to do shell script "cd " & my_posix_path & "&& xsltproc " & xsl_file & " " & xml_file
(*
"First item :p
2nd item"
*)
I also often have to parse XML structures to further process them with AppleScript.
I wrote myself a small tool named asxpath to conveniently execute XPath expressions against XML files.
Here is a short example:
-- path to the asxpath utility
set ctoolpath to quoted form of "/Users/martin/Desktop/asxpath"
-- path to the XML file
set xmlfilepath to quoted form of "/Users/martin/Desktop/test.xml"
-- XPath expression to isolate the needed info
set xpathexpr to quoted form of "//object/attribute[@name=\"site\"]/text()"
-- creating the complete command...
set command to ctoolpath & " -file " & xmlfilepath & " -xpath " & xpathexpr
-- ...and executing it on the command line
set output to paragraphs of (do shell script command)
-- returns {"First item :p", "...", "2nd item"}