Skip to main content

Namespace problem with DOMDocumentSerializer : IOException: namespace URI o

3 replies [Last post]
dmucha
Offline
Joined: 2006-11-24
Points: 0

I have a namespace problem with DOMDocumentSerializer : IOException: namespace URI of local name not indexed

I thought the DOM Level 2 compliant way to create the following document is

/*
* <?xml version="1.0" encoding="UTF-8"?>
*
*
*
*/

Document doc = db.newDocument();
Element root = doc.createElementNS("http://NewDefaultNamespaceURI.xxx.org", "root");
doc.appendChild(root);

Element e = doc.createElementNS("http://www.xxx.org", "ABC:e");
root.appendChild(e);

I can serialize this with com.sun.org.apache.xml.internal.serialize.XMLSerializer
and convert it to a string and everything is ok.

When I serialize it with com.sun.xml.fastinfoset.dom.DOMDocumentSerializer
into a fastinfoset document I get an IOException (complete unit test see below) :

java.io.IOException: namespace URI of local name not indexed: http://NewDefaultNamespaceURI.xxx.org
at com.sun.xml.fastinfoset.Encoder.encodeLiteralElementQualifiedNameOnThirdBit(Encoder.java:826)

So DOMDocumentSerializer shows a different behaviour than XMLSerializer with the same document.

When I add the namespace additionally as an attribute via setAttributeNS
the call to com.sun.xml.fastinfoset.dom.DOMDocumentSerializer.serialize succeeds :

Document doc = db.newDocument();
Element root = doc.createElementNS("http://NewDefaultNamespaceURI.xxx.org", "root");
root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "http://NewDefaultNamespaceURI.xxx.org");
doc.appendChild(root);

Element e = doc.createElementNS("http://www.xxx.org", "ABC:e");
e.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:ABC", "http://www.xxx.org");
root.appendChild(e);

It seems to me that DOMDocumentSerializer needs the namespace as an attribute
to do its task with DOM Level 2 elements.

The parser generates the additional attributes for the namespaces,
so the serializer works in a round trip environment.
Only if a program generated document is serialized the above problems occur.

Is this a bug in DOMDocumentSerializer?
Is there a way to index the namespace URI of local names automatically?

In the forum I found a message addressing attributes and namespaces http://forums.java.net/jive/thread.jspa?forumID=44&threadID=2585&message...
> Paul Sandoz wrote :
> The DOM serializer is currently a bit dumb and does not declare namespaces.
> It is necessary for the namespace declarations to already be declared.

Are the above mentioned "bug" and "The DOM serializer is currently a bit dumb" addressing the same problem?
If yes then when is that "dumbness" scheduled to get fixed?

Dietrich

public void testDoNotIgnoreDOMSerializationNamespaceProblem() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();

/*
create the following document :

<?xml version="1.0" encoding="UTF-8"?>

*/
Document doc = db.newDocument();

Element root = doc.createElementNS("http://NewDefaultNamespaceURI.xxx.org", "root");
// --> setAttributeNS is needed for ds.serialize(doc) to succeed
root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "http://NewDefaultNamespaceURI.xxx.org");
doc.appendChild(root);

Element e = doc.createElementNS("http://www.xxx.org", "ABC:e");
// --> setAttributeNS is needed for ds.serialize(doc) to succeed
e.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:ABC", "http://www.xxx.org");
root.appendChild(e);

DOMDocumentSerializer ds = new DOMDocumentSerializer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ds.setOutputStream(baos);

ds.setIgnoreComments(false);
ds.setIgnoreProcesingInstructions(false);
ds.setIgnoreWhiteSpaceTextContent(false);

ds.serialize(doc);

Document docOut = db.newDocument();
DOMDocumentParser parser = new DOMDocumentParser();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
parser.parse(docOut, bais);
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
oleksiys
Offline
Joined: 2006-01-25
Points: 0

Hi Dietrich,

it should be fixed now.

Alexey.

dmucha
Offline
Joined: 2006-11-24
Points: 0

Hi Alexey,

yes, it is fixed.
I confirmed it with the weekly build from December 2nd, 2006.

I closed the issue and hope that was correct (instead of setting it to verified).

Thank you for the quick fix.

Dietrich

sandoz
Offline
Joined: 2003-06-20
Points: 0

Hi Dietrich,

Yes, they are one and the same. Would it be possible for you to log an issue and state a date when you would like the "dumbness" removed? :-)

Thanks!
Paul.