• 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: XML script library?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: XML script library?


  • Subject: Re: XML script library?
  • From: Shane Stanley <email@hidden>
  • Date: Sun, 22 Feb 2015 17:26:41 +1100

On 22 Feb 2015, at 7:28 am, Jörgen Stahle <email@hidden> wrote:

Do anybody know if there is any stable script library available out there for making (and parsing) xml-data?

I have been using XMLlib and XML Tools earlier, but for my new project I would prefer to use a script library, if there is any. 

With AppleScriptObjC you have access to NSXMLDocument and friends, which also support XPath queries. Here's a sample of an XPath query:

use scripting additions
use framework "Foundation"

on extractFrom:thePath matchingXPath:theQuery
set anNSURL to current application's |NSURL|'s fileURLWithPath:thePath -- make NSURL
set {theNSXMLDocument, theNSError} to current application's NSXMLDocument's alloc()'s initWithContentsOfURL:anNSURL options:0 |error|:(reference) -- make XMLDoc
if theNSXMLDocument = missing value then -- there's a problem
display dialog (theNSError's localizedDescription()) as text
error number -128
end if
set attStrings to {} -- where attributes will be stored
set theXMLOutput to current application's NSXMLElement's alloc()'s initWithName:"output" -- found nodes added to this
set {theResults, theNSError} to (theNSXMLDocument's nodesForXPath:theQuery |error|:(reference)) -- query
if theResults is not missing value then
repeat with anNSXMLNode in (theResults as list)
anNSXMLNode's detach() -- need to detach first
if anNSXMLNode's |kind|() as integer = current application's NSXMLAttributeKind then -- see if it's an attribute or node
set end of attStrings to (anNSXMLNode's stringValue()) as text
else
(theXMLOutput's addChild:anNSXMLNode) -- add node
end if
end repeat
return {(theXMLOutput's XMLStringWithOptions:(current application's NSXMLNodePrettyPrint)) as text, attStrings} -- change options to suit
else
return missing value
end if
end extractFrom:matchingXPath:

(* This is an example of performing an XPath query, in this case retrieving the name of every item in your iTunes library. It returns an XML element called output, containing the matching nodes, plus a list of found attributes if that's what your XPath query requests *)

set thePath to POSIX path of (path to music folder) & "iTunes/iTunes Library.xml"
its extractFrom:thePath matchingXPath:"//key[text()='Name']/following-sibling::string[1]"


-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >XML script library? (From: Jörgen Stahle <email@hidden>)

  • Prev by Date: XML script library?
  • Next by Date: Re: Finder Passing Copies of Folders Rather than the Originals
  • Previous by thread: XML script library?
  • Next by thread: Re: XML script library?
  • Index(es):
    • Date
    • Thread