Keitaro84
3rd May 2023, 16:54
Hi all, I have this xml:

<?xml version="1.0"?>
<root>
<documents>
...
</documents>
</root>

in ln 10.7 is there a quick way to change the xml declaration? do I need to switch from <?xml version="1.0"?> to <?xml version="1.0" encoding="UTF-8"?>

I would like to know which is the best way because the file that is generated could have several lines.

Thank you very much!

Keitaro84
4th May 2023, 17:59
I've implemented this code, but I don't know if it's the best solution.

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)
}