You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Mikael St�ldal <d9...@d.kth.se> on 2000/08/13 16:30:31 UTC
Problem with SAX2 Serialization and namespaces in Xerces 1.1.3
The following sample program:
import java.io.*;
import org.apache.xml.serialize.*;
import org.xml.sax.*;
import org.xml.sax.helpers.AttributesImpl;
public class Serialize
{
public static void main(String[] args) throws Exception
{
if (args.length < 1)
{
System.out.println("Syntax: Serialize <filename>");
return;
}
OutputFormat of = new OutputFormat();
of.setEncoding("iso-8859-1");
of.setDoctype(null,null);
XMLSerializer serial =
new XMLSerializer(new FileOutputStream(args[0]), of);
ContentHandler ch = serial.asContentHandler();
ch.startDocument();
AttributesImpl atts = new AttributesImpl();
atts.addAttribute("http://www.foo.com/attrNs", "fruit", "",
"CDATA", "banana");
ch.startElement("http://www.foo.com/ns", "foo", "", atts);
ch.characters("foo bar".toCharArray(),0,7);
ch.endElement("http://www.foo.com/ns", "foo", "");
ch.endDocument();
}
}
Generates the following ill-formed output:
<?xml version="1.0" encoding="iso-8859-1"?>
< ="banana">foo bar</>
Perhaps the Xerces serialization module assume that the qname argument
to ContentHandler.startElement/endElement and
AttributesImpl.addAttrbute should be null when not present? But
according to the SAX2 documentation, it should be the empty string.
If I change the qname argument to null in the attribute, I'll get this
result:
<?xml version="1.0" encoding="iso-8859-1"?>
< fruit="banana">foo bar</>
If I change the qname argument to null in the element, I'll get:
java.lang.NullPointerException
at
org.apache.xml.serialize.XMLSerializer.startElement(XMLSerializer.java:224)
If I use ContentHandler.startPrefixDeclaration for both namespace URIs
and use null as qname argument for both element and attribute, then I
finally get the expected result:
<?xml version="1.0" encoding="iso-8859-1"?>
<ns:foo ans:fruit="banana" xmlns:ns="http://www.foo.com/ns" xmlns:ans="http://www.foo.com/attrNs">foo bar</ns:foo>
However, should it really be nessesary to use startPrefixDeclaration?
Isn't the serializer supposed to make up prefixes as needed? It should
of course use any startPrefixDeclaration bindings.
--
/****************************************************************\
* You have just read a message from Mikael Ståldal. *
* *
* Remove "-ingen-reklam" from the address before mail replying. *
\****************************************************************/