XML handling

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):

"<?xml version=\"1.0\" encoding=\"UTF-8\"?>

<plist version="1.0">


_dataType
SPHardwareDataType
_detailLevel
-2
_items


_name
hardware_overview
boot_rom_version
4.8.9f1
bus_speed
167 MHz
cpu_type
PowerPC G4 (1.2)
current_processor_speed
1.42 GHz
l2_cache_size
512 KB
machine_model
PowerMac10,1
machine_name
Macmini
number_processors
1
physical_memory
1 GB
serial_number
YM5175PGRHU


_properties

MMM_state

_order
70

boot_rom_release_date

_order
52

boot_rom_vendor

_order
50

boot_rom_version

_order
51

bus_speed

_order
45

cpu_type

_order
15

current_processor_speed

_order
21

l2_cache_size
etc. etc. etc.

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.

Hi,

I don’t know all the info you want, but you might want to look at this post: http://bbs.applescript.net/viewtopic.php?id=11372

For example:

--Processor speed in Mhz
set processor_speed to (system attribute "pclk") div 1000000

Seems a lot quicker than parsing a large XML file.

Best wishes

John M

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.)

Use XMLLib’s plist suite.

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

Thanks guys, I’ll look into it.