Re: XMLSerializer and default namespaces

Aleksander Slominski <[email protected]>
Newsgroups gmane.text.xml.xmlpull.devel
Message-ID <[email protected]>
Iain Shigeoka wrote:

>I've been using the XMLSerializer and really enjoy the API. The only feature
>that I feel would be really helpful is some way to tell the serializer that
>I'd prefer a certain start tag namespace declaration be a default namespace
>declaration rather than have it generate and use a prefix for itself and all
>children.
>
hi,

this is not immediately obvious but you can do it already: set default 
namespace by binding
namespace to prefix that is empty string, see last note in javadoc 
description of :setPrefix():

http://www.xmlpull.org/v1/doc/api/org/xmlpull/v1/XmlSerializer.html#setPrefix(java.lang.String,%20java.lang.String)

and find below a simple sample to demonstrate use of it. - when you run 
this sample
you should see output similar to this:

serializer implementation class is class 
org.xmlpull.mxp1_serializer.MXSerializer
serializer output: <foo 
xmlns="http://examlple.com/sample/namespace">bar</foo>

>The motivation is that some xml apps I deal with use their own, home grown
>xml parsers that aren't namespace aware. So they will often only match the
>bare tag name (prefixes throws them into a tizzy). This is most common with
>J2ME midlets although I've seen some desktop apps doing the same.
>
we all have sometimes to deal with imperfect XML - i can only sympathize 
with you :-)

alek

ps. next verion of XPP3/MXP1 (MXSerializer) will support all optional 
serialzier properties and features  to give greater control on output 
formatting 
(http://xmlpull.org/v1/doc/features.html#serializer-attvalue-use-apostrophe, 
http://xmlpull.org/v1/doc/properties.html#serializer-indentation, 
http://xmlpull.org/v1/doc/properties.html#serializer-line-separator)

ps2. here is example application:

import java.io.IOException;
import java.io.StringReader;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer;
import java.io.StringWriter;

public class Example7 {
    public static void main (String args[])
        throws XmlPullParserException, IOException
    {

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance(
            System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null);
            //"org.xmlpull.mxp1.MXParserFactory", null);
        XmlSerializer serializer = factory.newSerializer();
        StringWriter sw = new StringWriter();
        serializer.setOutput(sw);
        System.out.println("serializer implementation class is 
"+serializer.getClass());
        final String SAMPLE_NS = "http://examlple.com/sample/namespace";
        serializer.setPrefix("", SAMPLE_NS);
        serializer.startTag(SAMPLE_NS, "foo");
        serializer.text("bar");
        serializer.endTag(SAMPLE_NS, "foo");
        serializer.endDocument();
        System.out.println("serializer output: "+sw);
    }

}


-- 
"Mr. Pauli, we in the audience are all agreed that your theory is crazy. 
What divides us is whether it is crazy enough to be true." Niels H. D. Bohr



------------------------ Yahoo! Groups Sponsor ---------------------~-->
Get 128 Bit SSL Encryption!
http://us.click.yahoo.com/LIgTpC/vN2EAA/xGHJAA/2U_rlB/TM
---------------------------------------------------------------------~->

To unsubscribe from this group, send an email to:
[email protected]

 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.