XMLDocument.save("localfile.xml") <possible solutions>

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

XMLDocument.save("localfile.xml") <possible solutions>

Lucky-3
XMLDocument.prototype.save = function(localfile) {
   var C = Components.classes, I = Components.interfaces;

   var localFile =
C["@mozilla.org/file/local;1"].createInstance(I.nsILocalFile);
   localFile.initWithPath(localfile);
   if (!localFile.exists()) localFile.create(localFile.NORMAL_FILE_TYPE,
0644);

   var outputStream =
C["@mozilla.org/network/file-output-stream;1"].createInstance(I.nsIFileOutputStream);
   outputStream.init(localFile, 0x04 | 0x08 | 0x20, 0644, 0);
   //
<http://xulplanet.com/references/objref/XMLSerializer.html#method_serializeToStream>
   (new XMLSerializer).serializeToStream(this, outputStream, "UTF-8");
   outputStream.close();
}

XMLDocument.prototoype.save = function(localfile) {
   var C = Components.classes, I = Components.interfaces;

   var fos = C["@mozilla.org/file/local;1"].createInstance(I.nsILocalFile);
   fos.initWithPath(localfile);
   if (!fos.exists()) fos.create(fos.NORMAL_FILE_TYPE, 0644);

   var charset = "UTF-8"; // can be any character encoding name that
Mozilla supports
   var os =
C["@mozilla.org/intl/converter-output-stream;1"].createInstance(I.nsIConverterOutputStream);
   os.init(fos, charset, 0, 0x0000);
   //os.writeString("Umlaute: \u00FC \u00E4\n"); // direct JSString
access, not needed here.
   //os.writeString("Hebrew:  \u05D0 \u05D1\n");

   (new XMLSerializer).serializeToStream(this, os, charset);
   os.close();
}
_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml
Reply | Threaded
Open this post in threaded view
|

Re: XMLDocument.save("localfile.xml") <possible solutions>

Lucky-3
Lucky wrote:
> [sloppy source code here...] :)
sorry bout that, will also post as attachments..
slightly cleaned up code too


_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml

save.1.js (714 bytes) Download Attachment
save.js (772 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: XMLDocument.save("localfile.xml") <possible solutions>

Christian Biesinger
In reply to this post by Lucky-3
Lucky wrote:
>   if (!localFile.exists()) localFile.create(localFile.NORMAL_FILE_TYPE,
> 0644);

This is not needed as you pass PR_CREATE_FILE (0x08) to init.

>   outputStream.init(localFile, 0x04 | 0x08 | 0x20, 0644, 0);

not sure why this uses PR_RDWR (0x04) rather than PR_WRONLY (0x02)...

As for the second variant:

>   (new XMLSerializer).serializeToStream(this, os, charset);

This doesn't work, does it? The converter output stream is not an
nsIOutputStream...
_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml
Reply | Threaded
Open this post in threaded view
|

Re: XMLDocument.save("localfile.xml") <possible solutions>

Christian Biesinger
In reply to this post by Lucky-3
Lucky wrote:
> Lucky wrote:
>> [sloppy source code here...] :)
> sorry bout that, will also post as attachments..
> slightly cleaned up code too

Hm..

save.1.js looks right, except:
     outputSteam.close();
Should that be outputStream? ;)

But I don't understand why save.js works:
   var outputStream =
     Components.classes["@mozilla.org/intl/converter-output-stream;1"].
     createInstance(Components.interfaces.nsIConverterOutputStream);
   outputStream.init(file, (charset=charset||"UTF-8"), 0, 0x0000);

You can't initialize an nsIConverterOutputStream with a file.
(charset=charset also doesn't make much sense?)
_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml
Reply | Threaded
Open this post in threaded view
|

Re: XMLDocument.save("localfile.xml") <possible solutions>

Christian Biesinger
Christian Biesinger wrote:
> (charset=charset also doesn't make much sense?)

Oh... maybe it does. I'm not sure what the precedence of || and = is in JS.
_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml
Reply | Threaded
Open this post in threaded view
|

Re: XMLDocument.save("localfile.xml") <possible solutions>

Lucky-3
Christian Biesinger wrote:
> Christian Biesinger wrote:
>> (charset=charset also doesn't make much sense?)
>
> Oh... maybe it does. I'm not sure what the precedence of || and = is in JS.
i was just playing w/ the possibility of second argument to allow a dif
charset

so
.save("file.xml");
.save("file.xml", "UTF-8");

will result in same thing.
and just redefining it to charset, for the next use of it.
_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml
Reply | Threaded
Open this post in threaded view
|

Re: XMLDocument.save("localfile.xml") <possible solutions>

Lucky-3
In reply to this post by Christian Biesinger
Christian Biesinger wrote:

> Lucky wrote:
>>   if (!localFile.exists())
>> localFile.create(localFile.NORMAL_FILE_TYPE, 0644);
>
> This is not needed as you pass PR_CREATE_FILE (0x08) to init.
>
>>   outputStream.init(localFile, 0x04 | 0x08 | 0x20, 0644, 0);
>
> not sure why this uses PR_RDWR (0x04) rather than PR_WRONLY (0x02)...
>
> As for the second variant:
>
>>   (new XMLSerializer).serializeToStream(this, os, charset);
>
> This doesn't work, does it? The converter output stream is not an
> nsIOutputStream...
both seem to produce a valid file, but will test further.
i just pieced together some examples, so i'm trying to find the
BEST/SIMPLEST code that will achieve the results needed :)

by all means, unnecessary steps should be eliminated.
_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml
Reply | Threaded
Open this post in threaded view
|

Re: XMLDocument.save("localfile.xml") <possible solutions>

Lucky-3
Lucky wrote:

> Christian Biesinger wrote:
>> Lucky wrote:
>>>   if (!localFile.exists())
>>> localFile.create(localFile.NORMAL_FILE_TYPE, 0644);
>>
>> This is not needed as you pass PR_CREATE_FILE (0x08) to init.
>>
>>>   outputStream.init(localFile, 0x04 | 0x08 | 0x20, 0644, 0);
>>
>> not sure why this uses PR_RDWR (0x04) rather than PR_WRONLY (0x02)...
>>
>> As for the second variant:
>>
>>>   (new XMLSerializer).serializeToStream(this, os, charset);
>>
>> This doesn't work, does it? The converter output stream is not an
>> nsIOutputStream...
> both seem to produce a valid file, but will test further.
> i just pieced together some examples, so i'm trying to find the
> BEST/SIMPLEST code that will achieve the results needed :)
>
> by all means, unnecessary steps should be eliminated.
ok, my testing was quick and flawed, and it did not save out.
_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml