Ok, I want to extract data from the XML document created by the system_profiler -XML > data.xml command. I have a script for creating the file and reading it using XML Tools which looks as follows:
do shell script "system_profiler -xml > data.xml"
set xml_source to ((path to startup disk) as string) & "data.xml"
set xml_data to parse XML (read file xml_source)
Here is the layout of the document (I’m only including part of the HardwareDataType since the doc is quite large):
Is there a way to specify using XML Tools that I want for example the string after the first “current_processor_speed” key, which in this case would be 1.42 GHz? Note that a key with the same name is used later in the document, as well.
I have XMLLib installed too so if it’s much better to use that one you can provide a solution for it, instead. I simply found XML Tools first and I haven’t had a reason to really try out XMLLib yet.
Ah, that is interesting. I might need to parse the XML document anyway since I’m not sure if I can get all the information I’m looking for through such commands (I’m want pretty much all info given by the System Profiler), but this will certainly help.
Thanks!
(If anyone has any tips about parsing the document, feel free to mention it either way.)
You can specify which sections you want in the -xml, which is good, 'cause it’s long otherwise. This is an AppleScript I put together to get a list of all the main stuff. It uses the XSLT Tools.osax (it seemed that XMLlib.osax wanted the xml to be a file, which seemed unnecessary; but the xsl would work with either, as it’s plain xsl).
[P.S. I don’t know how to write the code for ASCII 10 here, it gets turned into a return.]
set theXML to do shell script "system_profiler SPHardwareDataType SPSoftwareDataType SPNetworkDataType SPMemoryDataType -xml" as Unicode text
set theXSL to "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">
<xsl:output method=\"text\" version=\"1.0\" encoding=\"utf-8\" indent=\"no\"/>
<xsl:template match=\"/\">
<xsl:for-each select=\"plist/array/dict/array/dict/key\"
<xsl:value-of select=\".\"/>
<xsl:text>: </xsl:text>
<xsl:value-of select=\"normalize-space(following-sibling::*)\"/>
<xsl:text>html code for ASCII 10</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>"
transform XML theXML using theXSL