Serialize XML without ?xml tag

N

Neil

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
 
L

Lew

Neil said:
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.
 
M

Mike Schilling

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.
 
N

Neil

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
 
M

Mike Schilling

Neil said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top