I've implemented this code, but I don't know if it's the best solution.
Code:
function long change.xml.file.declaration(domain tcmcs.str256m i.file.path) {
domain tcmcs.str256m o.file.path
long i.fp, o.fp, ret
string buffer(1024)
string temp.uuid(22)
string final.uuid(36)
boolean is.first
is.first = true
temp.uuid = uuid.generate$()
final.uuid = uuid.format$(temp.uuid)
o.file.path = bse.dir$() & path.dir.separator() & "purchases" & path.dir.separator() & "tmp" & path.dir.separator() & final.uuid & ".xml"
i.fp = seq.open(i.file.path, "r+")
o.fp = seq.open(o.file.path, "w+")
if (i.fp >= 1 and o.fp >= 0) then
while not seq.gets(buffer, 1024, i.fp, GETS_ALL_CHARS)
if(is.first) then
string new.buffer(1024)
new.buffer = str.replace$(buffer,
"<?xml version=" & chr$(34) & "1.0" & chr$(34) & "?>",
"<?xml version=" & chr$(34) & "1.0" & chr$(34) & " encoding=" & chr$(34) & "UTF-8" & chr$(34) & "?>")
is.first = false
new.buffer = trim$(new.buffer)
ret = seq.write(new.buffer, len(new.buffer), o.fp)
else
ret = seq.write(buffer, 1024, o.fp)
endif
endwhile
seq.close(i.fp)
seq.close(o.fp)
ret = file.cp(o.file.path, i.file.path)
ret = file.rm(o.file.path)
else
if i.fp > 0 then
seq.close(i.fp)
else
seq.close(o.fp)
endif
endif
return(ret)
}