stax and writing xml

I searched and didn't find anything on this. Hopefully it's a simple question.

I'm using stax to write xml to an output stream and I'd like to be able to generate self closed elements.

i.e. I want to create <foo id="2" name="bar"/>

not <foo id="2" name="bar"></foo>

The only way I can see to write it out is as follows:

writer.writeStartElement("foo" );

writer.writeAttribute("id","2");

writer.writeAttribute("name","bar" );

writer.writeEndElement();

which produces the empty closing tags.

Any ideas?

thanks

-k

[842 byte] By [kent_ha] at [2007-11-26 19:59:35]
# 1
I was just thinking I could do this with a bunch of writer.writeCharacters(string)if anyone has any better ideas fire away.thanks-k
kent_ha at 2007-7-9 22:56:23 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
Why do you want that? Both forms of empty element are equally well-formed XML and they are equivalent to each other.
DrClapa at 2007-7-9 22:56:23 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
Just fewer bytes to stream out to the client. This will be streaming back some fairly large data sets.I may just end up going with the empty closing tags as there appears to be no simple way to have the self closed tags.-k
kent_ha at 2007-7-9 22:56:23 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

In general, controlling this isn't easy, you may try using writeEmptyElement() but I don't believe it makes any guarantees about how closing tags are serialized.

If large data sets is a big problem in your application, and readability isn't a primary concern, you may try using Fast Infoset,

https://fi.dev.java.net

which is now part of the platform in JDK 6. Using this format you can improve your application's performance and reduce bandwidth requirements by half, without having to re-write your application. For example JAX-WS, the WS API, already supports this format. Morever, it includes an HTTP-based content negotiation algorithm to make the use of the format completely transparent.

spericasa at 2007-7-9 22:56:23 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...