Glen Daniels wrote:

Anyone have a clue if there is a way to use the XMLStreamWriter to get:

<elem attr="1"/>

instead of:

<elem attr="1"></elem>

Glen,

in short yes! XMLStreamWriter.writeEmptyElement() should be called from OM (see example below).


for elements with attributes but no content?

however i wonder why you care - from XML 1,0 and Infoset point of view thos2e two are equivalent!


If not, this seems VERY BAD.

:)

alek
BTW: to bad that solutoion from XmlPull API did nto survive in StAX i.e. it was intelligent to do <empty/> by default unless you called character("") in between.

ps. short example:

package samples;

import java.io.StringWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
/**
* Requires StAX RI 1.0 (JSR 173) available at http://stax.codehaus.org/
* @author Alek
*/
public class TestSerializer3
{
private final static String SOAP12 = "http://www.w3.org/2003/05/soap-envelope";; public static void doXmlOutput(boolean useRepairing) throws XMLStreamException {
       StringWriter buffer = new StringWriter();
       XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
       if (useRepairing) {
outputFactory.setProperty("javax.xml.stream.isRepairingNamespaces", Boolean.TRUE);
       }
       XMLStreamWriter out = outputFactory.createXMLStreamWriter(buffer);
       out.writeStartDocument();
       out.writeStartElement("env", "Envelope", SOAP12);
       out.writeNamespace("env", SOAP12);
       out.writeNamespace("test", "http://someTestUri";);
       out.writeEmptyElement("env", "Body", SOAP12);
       out.writeEndElement();
       out.writeEndDocument();
       out.close();
System.out.println("Created "+(useRepairing ? "" : "not")+" using repairing :-");
       System.out.println(buffer);
   }
public static void main(String[] s) throws Exception {
       doXmlOutput(false);
       doXmlOutput(true);
   }
}


--
The best way to predict the future is to invent it - Alan Kay

Reply via email to