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. “”, "), 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…
If you want to create XML files with AppleScript, then you might be interested in the free XML Tools from Late Night Software.
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
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