XMLDocument.save("localfile.xml");

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

XMLDocument.save("localfile.xml");

Lucky-3
Does anyone have the exact simplified source code to save an XMLDocument
locally after you've created it/modified it in DOM?

The target platform would be either a Firefox extension, or XUL App.

TIA
_______________________________________________
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");

Martin Honnen-3
Lucky wrote:

> Does anyone have the exact simplified source code to save an XMLDocument
> locally after you've created it/modified it in DOM?

I don't have the exact code but XMLSerializer can serialize to a string
or to a stream so you want the latter with a suitable stream
<http://xulplanet.com/references/objref/XMLSerializer.html#method_serializeToStream>

new XMLSerializer().serializeToStream(yourDocumentNode, stream, 'UTF-8')

If you want to write to a local file you can use e.g.

var localFile =
Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
localFile.initWithPath("C:\\temp\\localfile.xml");
if (!localFile.exists()) {
  localFile.create(localFile.NORMAL_FILE_TYPE, 0644);
}

var outputStream =
Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
outputStream.init(
   localFile,
   0x04 | 0x08 | 0x20,
   0644,
   0
);

Then pass that outputStream as the second argument to serializeToStream
and call close() on the stream afterwards.
Check the docs on XUL planet on those flags/permission numbers if you
need to adapt them for your needs.




--

        Martin Honnen
        http://JavaScript.FAQTs.com/
_______________________________________________
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");

Lucky-3
Martin Honnen wrote:

> Lucky wrote:
>
>> Does anyone have the exact simplified source code to save an
>> XMLDocument locally after you've created it/modified it in DOM?
>
> I don't have the exact code but XMLSerializer can serialize to a string
> or to a stream so you want the latter with a suitable stream
> <http://xulplanet.com/references/objref/XMLSerializer.html#method_serializeToStream>
>
>
> new XMLSerializer().serializeToStream(yourDocumentNode, stream, 'UTF-8')
>
> If you want to write to a local file you can use e.g.
>
> var localFile =
> Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
>
> localFile.initWithPath("C:\\temp\\localfile.xml");
> if (!localFile.exists()) {
>  localFile.create(localFile.NORMAL_FILE_TYPE, 0644);
> }
>
> var outputStream =
> Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
>
> outputStream.init(
>   localFile,
>   0x04 | 0x08 | 0x20,
>   0644,
>   0
> );
>
> Then pass that outputStream as the second argument to serializeToStream
> and call close() on the stream afterwards.
> Check the docs on XUL planet on those flags/permission numbers if you
> need to adapt them for your needs.
>
>
>
>
sounds good will try it out.
but i was under the impression that using the IO write methods will only
produce an ANSI output? and that UTF-8 characters will not work??

_______________________________________________
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");

Wired Earp-3
Lucky wrote

> i was under the impression that using the IO write methods will
> only produce an ANSI output? and that UTF-8 characters will not work??

You can strap on a nsIScriptableUnicodeConverter or use the new method
described over at http://developer.mozilla.org/en/docs/Writing_textual_data


--
Wired Earp
Wunderbyte

_______________________________________________
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");

Martin Honnen-3
In reply to this post by Lucky-3
Lucky wrote:

> but i was under the impression that using the IO write methods will only
> produce an ANSI output? and that UTF-8 characters will not work??

I might be wrong but my understanding is that passing JavaScript strings
  to some IO write methods is not capable of treating Unicode properly
as those write methods might expect bytes and those JavaScript strings
being sequences of Unicode characters get truncated that way.
The code however that I suggested simply passes the document node to an
XMLSerializer where serialization is done to a stream, there is no
JavaScript string passed to a write method expecting bytes that way.


--

        Martin Honnen
        http://JavaScript.FAQTs.com/
_______________________________________________
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");

Lucky-3
Martin Honnen wrote:

> Lucky wrote:
>
>> but i was under the impression that using the IO write methods will
>> only produce an ANSI output? and that UTF-8 characters will not work??
>
> I might be wrong but my understanding is that passing JavaScript strings
>  to some IO write methods is not capable of treating Unicode properly as
> those write methods might expect bytes and those JavaScript strings
> being sequences of Unicode characters get truncated that way.
> The code however that I suggested simply passes the document node to an
> XMLSerializer where serialization is done to a stream, there is no
> JavaScript string passed to a write method expecting bytes that way.
>
>
great thanks, will give it some tries.
thanks for the response(s)

the while components/interfaces is a new twist for me, i'm familiar w/
ms scripting IO's primarily, and they support [XMLDOM].save("file.xml");
within the ActiveX control, so it's never had to be exposed/written by
myself that way.
_______________________________________________
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");

Lucky-3
In reply to this post by Wired Earp-3
Wired Earp wrote:
> Lucky wrote
>
>> i was under the impression that using the IO write methods will
>> only produce an ANSI output? and that UTF-8 characters will not work??
>
> You can strap on a nsIScriptableUnicodeConverter or use the new method
> described over at http://developer.mozilla.org/en/docs/Writing_textual_data
>
>
great, funny you mentioned this, for i just came up on that bit myself.
however, i'm still a bit upset w/ that type of documentation, it assumes
you know how to fully put all those pieces in the right order, and my
skills in nsInterfaces is a bit infantile so far.

i think w/ all the help i might piece meal it together.

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