Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Serialize XML without ?xml tag

Reply
Thread Tools

Serialize XML without ?xml tag

 
 
Neil
Guest
Posts: n/a
 
      09-27-2009
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.
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      09-27-2009
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
 
Reply With Quote
 
 
 
 
Mike Schilling
Guest
Posts: n/a
 
      09-27-2009
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.



 
Reply With Quote
 
Neil
Guest
Posts: n/a
 
      09-28-2009
On Sep 27, 6:42*pm, "Mike Schilling" <(E-Mail Removed)>
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.



 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      09-28-2009
Neil wrote:
> On Sep 27, 6:42 pm, "Mike Schilling" <(E-Mail Removed)>
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing an XML file and adding another tag, if the tag is not available / the value is null P Perl Misc 7 01-12-2007 03:28 AM
only a simple xml reader <tag:id>value</tag:id> martijn@gamecreators.nl Python 4 02-11-2006 06:44 PM
how do u invoke Tag b's Tag Handler from within Tag a's tag Handler? shruds Java 1 01-27-2006 03:00 AM
serialize to SQL Server Blob instead of XML serialize Gordz ASP .Net 3 06-07-2004 07:46 PM
Can I make a class from XML? Serialize? Jonah Olsson ASP .Net 12 08-09-2003 04:18 PM



Advertisments