Serialize XML without ?xml tag

Discussion in 'Java' started by Neil, Sep 27, 2009.

  1. Neil

    Neil Guest

    Hello:

    I am using LSSerializer to serialize an XML document to a string.
    It generates this tag at the top of this string:
    <?xml version="1.0" encoding="UTF-16"?>

    I need to generate the XML without that.

    How can I do that?

    Thanks,
    Neil

    --
    Neil Aggarwal, (281)846-8957, www.JAMMConsulting.com
    Will your e-commerce site go offline if you have
    a DB server failure, fiber cut, flood, fire, or other disaster?
    If so, ask about our geographically redundant database system.
     
    Neil, Sep 27, 2009
    #1
    1. Advertising

  2. Neil

    Lew Guest

    Neil wrote:
    > I am using LSSerializer to serialize an XML document to a string.
    > It generates this tag at the top of this string:
    > <?xml version="1.0" encoding="UTF-16"?>
    >
    > I need to generate the XML without that.
    >
    > How can I do that?


    Strictly speaking, you can't. If you don't have the XML declaration, it isn't
    legally XML.

    I also don't think it's possible using an 'LSSerializer'. I'm not familiar
    with that interface since normally I don't code nuts-and-bolts for XML the way
    I guess you're doing but rather use java.xml.whatever stuff or higher-level
    frameworks like JAXB. But reviewing its Javadocs, I don't see the kind of
    control you're asking for.

    You might have to postprocess your String.

    --
    Lew
     
    Lew, Sep 27, 2009
    #2
    1. Advertising

  3. Lew wrote:
    > Neil wrote:
    >> I am using LSSerializer to serialize an XML document to a string.
    >> It generates this tag at the top of this string:
    >> <?xml version="1.0" encoding="UTF-16"?>
    >>
    >> I need to generate the XML without that.
    >>
    >> How can I do that?



    You could use XSLT to do the serialization (using an identity
    transformation) and tell that to omit the XML declaration.

    >
    > Strictly speaking, you can't. If you don't have the XML declaration,
    > it isn't legally XML.


    Not true; the declaration is not required. See
    http://www.xml.com/axml/testaxml.htm

    document ::= prolog element Misc*
    prolog ::= XMLDecl? Misc* (doctypedecl Misc*)?

    where the "?" means "optional". It would be necessary for some encodings,
    but UTF-16 is indicated by the BOM.
     
    Mike Schilling, Sep 28, 2009
    #3
  4. Neil

    Neil Guest

    On Sep 27, 6:42 pm, "Mike Schilling" <>
    wrote:
    > You could use XSLT to do the serialization (using an identity
    > transformation) and tell that to omit the XML declaration.


    I tried changing my code to use the TrAX transformer since it has
    a property to omit the xml declaration:

    Transformer transformer = TransformerFactory.newInstance
    ().newTransformer();
    transformer.setOutputProperty("indent","yes");
    transformer.setOutputProperty("omit-xml-declaration","yes");
    DOMSource source = new DOMSource(document);
    Result result = new StreamResult(output);
    transformer.transform (source, result);

    But, now it outputs the XML with one tag per line. The lines are not
    indented
    to show nesting even though I set indent to yes. Here is a sample:

    <Tag1>
    <Tag2>A</Tag2>
    <Tag3>B</Tag3>
    </Tag1>

    I want it to appear as:
    <Tag1>
    <Tag2>A</Tag2>
    <Tag3>B</Tag3>
    </Tag1>

    Any ideas how to make that happen?

    Thanks,
    Neil

    --
    Neil Aggarwal, (281)846-8957, www.JAMMConsulting.com
    Will your e-commerce site go offline if you have
    a DB server failure, fiber cut, flood, fire, or other disaster?
    If so, ask about our geographically redundant database system.
     
    Neil, Sep 28, 2009
    #4
  5. Neil wrote:
    > On Sep 27, 6:42 pm, "Mike Schilling" <>
    > wrote:
    >> You could use XSLT to do the serialization (using an identity
    >> transformation) and tell that to omit the XML declaration.

    >
    > I tried changing my code to use the TrAX transformer since it has
    > a property to omit the xml declaration:
    >
    > Transformer transformer = TransformerFactory.newInstance
    > ().newTransformer();
    > transformer.setOutputProperty("indent","yes");
    > transformer.setOutputProperty("omit-xml-declaration","yes");
    > DOMSource source = new DOMSource(document);
    > Result result = new StreamResult(output);
    > transformer.transform (source, result);
    >
    > But, now it outputs the XML with one tag per line. The lines are
    > not
    > indented
    > to show nesting even though I set indent to yes. Here is a sample:
    >
    > <Tag1>
    > <Tag2>A</Tag2>
    > <Tag3>B</Tag3>
    > </Tag1>
    >
    > I want it to appear as:
    > <Tag1>
    > <Tag2>A</Tag2>
    > <Tag3>B</Tag3>
    > </Tag1>
    >
    > Any ideas how to make that happen?


    Sorry, I don't know.
     
    Mike Schilling, Sep 28, 2009
    #5
    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. Jonah Olsson

    Can I make a class from XML? Serialize?

    Jonah Olsson, Aug 8, 2003, in forum: ASP .Net
    Replies:
    12
    Views:
    503
  2. Gordz
    Replies:
    3
    Views:
    3,940
    John Oakes
    Jun 7, 2004
  3. shruds
    Replies:
    1
    Views:
    834
    John C. Bollinger
    Jan 27, 2006
  4. Replies:
    4
    Views:
    443
  5. P
    Replies:
    7
    Views:
    141
    Tad McClellan
    Jan 12, 2007
Loading...

Share This Page