axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Aleksander Slominski <as...@cs.indiana.edu>
Subject Re: [axis2] Writing an empty element?
Date Wed, 31 Aug 2005 11:31:10 GMT
Glen Daniels wrote:

> Hi 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.
>>>
>>>
>>> +1, that's what Axis 1 does as well.
>>
>>
>> hmm,  i will take a look and see if i can change this for StAX RI - 
>> after all infoset is equivalent ...
>
>
> +1!  That would rock.

please pull StAX RI from SVN (http://stax.codehaus.org/Home) and build 
jar file (ant).

then if you run test program (below) it should be producing this:

Created not using repairing :-
<?xml version='1.0' encoding='utf-8'?><env:Envelope 
xmlns:env="http://www.w3.org/2003/05/soap-envelope" 
xmlns:test="http://someTestUri"><env:Body><test foo="bar"/><test 
foo="bar"></test><test foo="bar"> </test></env:Body></env:Envelope>
Created  using repairing :-
<?xml version='1.0' encoding='utf-8'?><env:Envelope 
xmlns:env="http://www.w3.org/2003/05/soap-envelope" 
xmlns:test="http://someTestUri" xmlns:test="http://someTestUri" 
xmlns:env="http://www.w3.org/2003/05/soap-envelope"><env:Body><test 
foo="bar"/><test foo="bar"></test><test foo="bar"> 
</test></env:Body></env:Envelope>

thanks.

alek

-----
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/Download
 * @author Alek
 */
public class TestSerializeEmptyEl
{
    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.writeStartElement("env", "Body", SOAP12);
       
        out.writeStartElement("test");
        out.writeAttribute("foo", "bar");
        out.writeEndElement();
       
        out.writeStartElement("test");
        out.writeAttribute("foo", "bar");
        out.writeCharacters("");
        out.writeEndElement();

        out.writeStartElement("test");
        out.writeAttribute("foo", "bar");
        out.writeCharacters(" ");
        out.writeEndElement();
       
        out.writeEndElement();
        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


Mime
View raw message