• 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 attributes
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: XML attributes


  • Subject: Re: XML attributes
  • From: Shane Stanley <email@hidden>
  • Date: Sat, 24 Nov 2018 23:07:00 +1100

On 24 Nov 2018, at 10:12 pm, Jörgen Stahle <email@hidden> wrote:
>
> I wonder if Shane or anyone else could help me with a fix for that…

Would something like this work for you:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

on makeXMLDocWithRecord:theRecord withRoot:rootName
        -- make root element
        set rootElement to current application's NSXMLNode's
elementWithName:rootName
        -- 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
        its addChildOrAttributesTo:rootElement withDictionary:anNSDictionary
        -- return as string with whatever formatting options you want
        return (theXMLDocument's XMLStringWithOptions:(current application's
NSXMLNodePrettyPrint)) as text
end makeXMLDocWithRecord:withRoot:

on addChildOrAttributesTo:parentElement withDictionary:theDict
        set theKeys to theDict's allKeys() as list
        repeat with i from 1 to count of theKeys
                set theKey to item i of theKeys
                set theValue to (theDict's objectForKey:theKey)
                if theKey as text = "attributes" then
                        (parentElement's setAttributesWithDictionary:theValue)
                else if theKey as text = "name_and_attributes" then
                        set newDict to (theDict's objectForKey:theKey)
                        set newName to (newDict's objectForKey:"name")
                        set theAtts to (newDict's objectForKey:"attributes")
                        (parentElement's setAttributesWithDictionary:theAtts)
                        (parentElement's setStringValue:newName)
                else
                        set newElement to (current application's NSXMLNode's
elementWithName:theKey)
                        (parentElement's addChild:newElement)
                        if (theValue's isKindOfClass:(current application's
NSDictionary)) as boolean then
                                (its addChildOrAttributesTo:newElement
withDictionary:theValue)
                        else
                                (newElement's setObjectValue:theValue)
                        end if
                end if
        end repeat
end addChildOrAttributesTo:withDictionary:

set theRecord to {firstName:"Saga", lastName:"Norén",
city:{name_and_attributes:{|name|:"Malmö", attributes:{country:"Sweden"}}},
partner:{firstName:"Martin", lastName:"Rohde",
city:{name_and_attributes:{|name|:"København",
attributes:{country:"Denmark"}}}, attributes:{approach:"dogged"}}}
its makeXMLDocWithRecord:theRecord withRoot:"character"


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


 _______________________________________________
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

  • Follow-Ups:
    • Re: XML attributes
      • From: Shane Stanley <email@hidden>
References: 
 >XML attributes (From: Jörgen Stahle <email@hidden>)

  • Prev by Date: XML attributes
  • Next by Date: Re: XML attributes
  • Previous by thread: XML attributes
  • Next by thread: Re: XML attributes
  • Index(es):
    • Date
    • Thread