DOMDocumentSerializer : namespace problem
Tomas-Kraus opened this issue · comments
I have a namespace problem with DOMDocumentSerializer : IOException: namespace
URI of local name not indexed
(found on Windows XP, probably an all platform all OS issue)
I thought the DOM Level 2 compliant way to create the following document is
/*
- <ABC:e xmlns:ABC="http://www.xxx.org"/>
- */
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.
In the forum I found a message addressing attributes and namespaces
http://forums.java.net/jive/thread.jspa?forumID=44&threadID=2585&messageID=36634#36634
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.
Dietrich
public void testDoNotIgnoreDOMSerializationNamespaceProblem() throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
/*
create the following document :
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);
}
Environment
Operating System: Windows XP
Platform: PC
Affected Versions
[current]
- Issue Imported From: https://github.com/javaee/metro-fi/issues/10
- Original Issue Raised By:@glassfishrobot
- Original Issue Assigned To: @glassfishrobot
- Original Issue Closed By:@glassfishrobot
@glassfishrobot Commented
Reported by dmucha
@glassfishrobot Commented
oleksiys said:
Taking care of issue.
Element NS will work now.
But still need fix for attribute namespaces (in process)
@glassfishrobot Commented
oleksiys said:
mentioned usecase should work now.
@glassfishrobot Commented
dmucha said:
I checked the issue with the weekly build from December, 2nd 2006.
The unit test now runs flawlessly.
Thank you for the quick work.
@glassfishrobot Commented
Was assigned to oleksiys
@glassfishrobot Commented
This issue was imported from java.net JIRA FI-10
@glassfishrobot Commented
Marked as fixed on Sunday, December 3rd 2006, 10:54:20 pm