• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to work with XML data using System Events?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to work with XML data using System Events?


  • Subject: Re: How to work with XML data using System Events?
  • From: "Steven D. Majewski" <email@hidden>
  • Date: Thu, 3 Nov 2005 13:36:15 -0500


On Nov 1, 2005, at 12:17 PM, pete boardman wrote:

The System Events dictionary contains these commands:

XML attribute
XML data
XML element
XML file

How would I use these to read XML data from an XML file? I've googled to no avail...


[ There has been a little discussion on this list -- you might try search via
http://search.lists.apple.com/ rather than google. ]



You can make an XML data object by passing it a string as a text property
( You can also pass a name property (* [See Note])). Example:


  tell application "System Events"
	make new XML data with properties {text:read (choose file)}
	...

You can make an XML file object with a file reference. The contents of
the XML file objects is the XML data objects. So the equivalent to the
above example would be:

  tell application "System Events"
	get contents of XML file ((choose file) as string)
	...

[ The first method is handy if you need to grab the xml from a 'do shell script' ]


The XML data object contains XML elements. It doesn't pass the "<?xml ... " as an element, and I'm guessing that it doesn't pass any PI's or declaration, so at the top level, you should only expect a single XML element.


As you can see from the System Events dictionary, an XML element can contain XML attributes and other XML elements, and can have a name and a value.

Once you've drilled down to the correct level, you may be able to collect a
bunch of values with an expression like (example):


	set pidlis to value of every XML element of result whose name is "pid"



Attributes also have a name and a value, so you can collect name, attributes
and simple string value ( not descending into any enclosed elements ) with
something like:


to elemstrs(xel)
tell application "System Events"
set str to ""
repeat with attr in XML attributes of xel
set str to space & str & (name of attr) & "=" & (value of attr) & space
end repeat

return {name of xel as string, str, value of xel as string}
end tell
end elemstrs




I may try to put together an interactive XML browser as a demo, however, after
finally figuring it out this far, I think:
(1) If it's something simple, it's easier to extract stuff with a perl regex.
(2) If it's something complicated, it may be easier to you xsltproc ( via
do shell script ) or something where you can just use an xpath
expression.



NOTE (*) Re: name properties: See a previous thread about the fact that System Events
hangs onto these objects which can cause some problems. If you use the 'make new
XML data' you need to delete them after you're done. The XML file method doesn't
seem to leave objects behind as far as I've noticed. These dangling objects aren't
just a memory leak -- the big problem is that if System Events has two XML objects
with the same name, the sort of references it returns seems to be indeterminate
with respect to which one it'll return. Leaving out the name parameter seems to
cause System Events to create unique names like "untitled 2" .



-- Steve Majewski


_______________________________________________ Do not post admin requests to the list. They will be ignored. Applescript-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: How to work with XML data using System Events?
      • From: pete boardman <email@hidden>
    • Re: How to work with XML data using System Events?
      • From: Emmanuel <email@hidden>
References: 
 >How to work with XML data using System Events? (From: pete boardman <email@hidden>)

  • Prev by Date: Re: Records
  • Next by Date: Re: Folder Actions on a Server
  • Previous by thread: Re: How to work with XML data using System Events?
  • Next by thread: Re: How to work with XML data using System Events?
  • Index(es):
    • Date
    • Thread