encoding="UTF-8" being deleted when generating file from DOM

Discussion in 'XML' started by Mike Kennedy, Nov 26, 2003.

  1. Mike Kennedy

    Mike Kennedy Guest

    I have an XML Snippet <?xml version="1.0" encoding="UTF-8"?> and when I
    convert the entire xml file to a DOM and then generate a new file from
    the DOM, results in <?xml version="1.0"?>.
    Any thoughts?

    My sample code snippet is:

    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(in.xml);
    FileWriter file = new FileWriter(out.xml);

    // Create an XMLInputStream using the DOM we've changed

    XMLInputStreamFactory ifactory =
    XMLInputStreamFactory.newInstance();
    XMLInputStream istream = ifactory.newInputStream(doc);

    // Create an XMLOutputStream to generate a new output file
    // using the file created earlier

    XMLOutputStreamFactory ofactory =
    XMLOutputStreamFactory.newInstance();
    XMLOutputStream output =
    ofactory.newOutputStream(new PrintWriter(file,true));
    output.add(istream);
    output.flush();
    output.close();
    Mike Kennedy, Nov 26, 2003
    #1
    1. Advertising

  2. Mike Kennedy wrote:

    > I have an XML Snippet <?xml version="1.0" encoding="UTF-8"?> and when I
    > convert the entire xml file to a DOM and then generate a new file from
    > the DOM, results in <?xml version="1.0"?>.
    > Any thoughts?


    That is allowed, encoding UTF-8 or UTF-16 has to recognized by any XML
    parser without the encoding being specified.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Nov 26, 2003
    #2
    1. Advertising

  3. Mike Kennedy

    Mike Kennedy Guest

    Martin, thanks. But I'm wondering why the encoding="UTF-8" is being
    stripped when the DOM is used to regenerate the xml. It's there in the
    original xml file and then when I generate a new xml doc from the DOM,
    it's not there.

    --Mike

    Martin Honnen wrote:
    >
    >
    > Mike Kennedy wrote:
    >
    >> I have an XML Snippet <?xml version="1.0" encoding="UTF-8"?> and when
    >> I convert the entire xml file to a DOM and then generate a new file
    >> from the DOM, results in <?xml version="1.0"?>.
    >> Any thoughts?

    >
    >
    > That is allowed, encoding UTF-8 or UTF-16 has to recognized by any XML
    > parser without the encoding being specified.
    >
    Mike Kennedy, Nov 29, 2003
    #3
  4. In article <ZKTxb.131$>,
    Mike Kennedy <> wrote:

    >Martin, thanks. But I'm wondering why the encoding="UTF-8" is being
    >stripped when the DOM is used to regenerate the xml.


    Because it makes no difference to the document. Just as if you had
    six spaces between attributes in the input, there's no reason to
    suppose that there will be six spaces in the output.

    -- Richard
    --
    Spam filter: to mail me from a .com/.net site, put my surname in the headers.

    FreeBSD rules!
    Richard Tobin, Nov 29, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gerlar2000
    Replies:
    0
    Views:
    616
    gerlar2000
    Feb 21, 2005
  2. Chumley the Walrus
    Replies:
    2
    Views:
    250
    Tom Gosselin
    Aug 10, 2004
  3. Addy
    Replies:
    1
    Views:
    139
    Alan J. Flavell
    Feb 18, 2004
  4. Yohan N. Leder

    How to mark UTF-8 string as being UTF-8

    Yohan N. Leder, Jun 2, 2006, in forum: Perl Misc
    Replies:
    9
    Views:
    122
    Alan J. Flavell
    Jun 5, 2006
  5. Replies:
    2
    Views:
    364
Loading...

Share This Page