Thursday, May 28, 2020

#1 2008-10-21 04:32:56 pm

aldek
Member
From:: Grodzisk Mazowiecki, Poland
Registered: 2008-02-29
Posts: 21

Creating new strict xml files with AppleScript

I have several variables, i.e. title, subtitle, text_body, edition_date...
I must send this data to database server. I want to create a single xml file with this variables as data. This solves main problems, i.e. delimiting and encoding.
But I dont now, how, in simple way, create xml files without adding in script text representation of tags (i.e. "<title>", </title>"), and header. System Events can do it?
I was reading this excellent forum, but i found only post with parsing, not creating a new xml files...

Best wishes

Offline

 

#2 2008-10-22 02:48:39 am

Martin Michel
Administrator
From:: Berlin, Germany
Registered: 2008-03-03
Posts: 701
Website

Re: Creating new strict xml files with AppleScript

Hi aldek,

If you want to create XML files with AppleScript, then you might be interested in the free XML Tools from Late Night Software.

aldek wrote:

But I dont now, how, in simple way, create xml files without adding in script text representation of tags (i.e. "<title>", </title>"), and header. System Events can do it?


I don't understand you here. There is no magic in creating a valid XML file. You just need the proper header, the right encoding (e.g. UTF-8) and correct XML syntax. All this can be easily done with AppleScript. Parsing XML is much more difficult (e.g. when it comes to complex plist files containing lists and arrays), that's why there are so many tools for it :-)

Applescript:


on run
   set filepath to (((path to desktop) as Unicode text) & "sample.xml")
   set xmldata to xmlheader() & return
   set xmldata to xmldata & "<note>" & return
   set xmldata to xmldata & my totag("Wife") & return
   set xmldata to xmldata & my bodytag("Fetch daughter from school") & return
   set xmldata to xmldata & "</note>"
   my writetofile(xmldata, filepath)
end run

on xmlheader()
   return "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
end xmlheader

on totag(txt)
   return "<to>" & txt & "</to>"
end totag

on bodytag(txt)
   return "<body>" & txt & "</body>"
end bodytag

on writetofile(cont, filepath)
   try
       set openfile to open for access filepath with write permission
       set eof of openfile to 0
       set BOM_UTF8 to ((ASCII character 239) & (ASCII character 187) & (ASCII character 191))
       write BOM_UTF8 to openfile
       write cont to openfile as «class utf8»
       close access openfile
       return true
   on error
       try
           close access openfile
       end try
       return false
   end try
end writetofile


Plonum - Convert images to iWork Numbers documents

Offline

 

#3 2008-10-23 03:32:44 am

aldek
Member
From:: Grodzisk Mazowiecki, Poland
Registered: 2008-02-29
Posts: 21

Re: Creating new strict xml files with AppleScript

Thank you, this handlers work perfectly.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)