• 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, 01 Mar 2015 22:57:46 +1100

On 1 Mar 2015, at 7:41 pm, Jörgen Stahle <email@hidden> wrote:

Parsing:
I don’t know Objective-C/Cocoa, and learning ASObjC would take some time that I don’t have right now.

You don't have to know Objective-C/Cocoa, and the basics of ASObjC are pretty simple. 

Writing:
However in my current project, I need to write XML. I like the method of having applescript records translated to XML – something like the solution for writing Json that you provided  in Macscripter forums, or the way XML Tools does the job.

Do you mean something like this:

use framework "Foundation"

on makeXMLDocWithRecord:theRecord
-- make root element
set rootElement to current application's NSXMLNode's elementWithName:"dict"
-- make XML document
set theXMLDocument to current application's NSXMLDocument's alloc()'s initWithRootElement:rootElement
theXMLDocument's setDocumentContentKind:(current application's NSXMLDocumentXMLKind)
theXMLDocument's setStandalone:true
theXMLDocument's setCharacterEncoding:"UTF-8"
-- make dictionary from record
set anNSDictionary to current application's NSDictionary's dictionaryWithDictionary:theRecord
-- add children to root element
set theKeys to anNSDictionary's allKeys() as list
repeat with i from 1 to count of theKeys
set theKey to item i of theKeys
set newElement to (current application's NSXMLNode's elementWithName:"key" stringValue:theKey)
(rootElement's addChild:newElement)
set newElement to (current application's NSXMLNode's elementWithName:"string" stringValue:(anNSDictionary's valueForKey:theKey))
(rootElement's addChild:newElement)
end repeat
-- return as string with whatever formatting options you want
return (theXMLDocument's XMLStringWithOptions:(current application's NSXMLNodePrettyPrint)) as text
end makeXMLDocWithRecord:

set theRecord to {firstName:"Saga", lastName:"Norén", city:"Malmö"}
its makeXMLDocWithRecord:theRecord

Result of Main Script:
  --> "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>
<dict>
    <key>firstName</key>
    <string>Saga</string>
    <key>city</key>
    <string>Malmö</string>
    <key>lastName</key>
    <string>Norén</string>
</dict>"

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

  • Next by Date: Re: XML script library?
  • Next by thread: Re: XML script library?
  • Index(es):
    • Date
    • Thread