I have a test case where when I get to this line in my script…
set theXML to parse XML theContent with including empty elements and allowing leading whitespace
…Script Debugger stops with an AppleScript Runtime Error:
XML parsing error: not well-formed at character 26 of line 33
Line 33 is as follows…
<ADDRESS1>Espigolera nº1, Pol Ind nº1, 08960, Sant Just Desvern</ADDRESS1>
I take it that the XML parse is barfing on the extended character, if I edit the XML file and remove the fancy letter after the "n"s then my script can proceed past the XML parse.
What do I need to do to get past this problem (editing of the XML before it is processed is not possible in the real world)?
Yes, the moment after I posted my question I changed the encoding of the XML file in TextWrangler from MacRoman to UTF-8 and then Script Debugger stopped complaining.
The script line that writes the XML file to begin with is…
to writeFile(theContent)
tell application "Finder"
set theFile to (folderpath & "Order_[" & ORDER_NUMBER & "]_backup.xml")
set f to (open for access theFile with write permission)
set eof of f to 0
try
write theContent to f as «class utf8»
close access f
on error errMsg
try
close access f
end try
error errMsg
end try
end tell
end writeFile
How would I be sure that the XML file’s text encoding was UTF-8?
I tried your code to read the file as UTF-8 with the XML file still showing as Mac Roman in TextWrangler and Script Debugger died on the read… Can’t make result into the expected type.
It seems my problem is that when the XML file is written the file isn’t written as UTF-8.
This is odd, normally writing as «class utf8» writes real UTF-8 encoded files.
You can try to add the UTF-8 BOM at the beginning of the file
to writeFile(theContent)
tell application "Finder"
set theFile to (folderpath & "Order_[" & ORDER_NUMBER & "]_backup.xml")
set f to (open for access theFile with write permission)
set eof of f to 0
try
write «data rdatEFBBBF» to f
write theContent to f as «class utf8» starting at eof
close access f
on error errMsg
try
close access f
end try
error errMsg
end try
end tell
end writeFile