Looking further into this problem, this issue arises from the way that
the setContentAsDOM method serialises the DOM node to obtain a text
string. That serialisation is done with the TextWriter class, which
seems to be a roll-your-own serialisation class that largely ignores
namespace declarations in the serialisation process. This means that
when the string needs to be parsed as XML, it is not valid XML.
I can get around this easily enough by using the Xerces XML serialiser
as per the method below.
/**
* Convert a DOM object to a string.
* @param dom The DOM object to convert to a string.
* @return The string that is the serialised content of the DOM
object.
* @throws XBRLException if an IO exception occurs.
*/
private String DOM2String(Document node) throws XBRLException {
try {
StringWriter sw = new StringWriter();
org.apache.xml.serialize.OutputFormat format = new
org.apache.xml.serialize.OutputFormat("xml", "UTF-8", true);
org.apache.xml.serialize.XMLSerializer output = new
org.apache.xml.serialize.XMLSerializer(sw, format);
output.setNamespaces(true);
output.serialize(node);
sw.flush();
String out = sw.toString();
sw.close();
return out;
} catch (IOException e) {
...
}
}
This seems to handle all of the namespace issues nicely enough (though I
have not tested passing in any DOM node rather than passing in a
Document node), leaving open the question: why not use this (or the
Xalan 2.7 serialiser when it is released next month [the 2.6 Xalan
serialiser has some namespace issues of its own]) instead of using the
TextWriter class?
Cheers
Geoff Shuetrim
On Tue, 2005-06-21 at 19:01 +1000, Geoffrey Shuetrim wrote:
> I am using the setContentAsDOM method provided by XMLResource instances
> to specify the content that I wish to insert into an Xindice database
> but am running into problems if the DOM object that is provided as an
> argument has had nodes created using the createElementNS or
> setAttributeNS methods.
>
> The attached source code provides two JUnit tests. One works because it
> does not have namespace declarations for the nodes in the document. The
> other fails because it does. The tests assume that the database URI is:
> xmldb:xindice://localhost:8180/db
>
> The problem occurs at the point where the database manager attempts to
> insert the XMLResource into the relevant collection, complaining about
> difficulties parsing the document.
>
> My environment is as follows:
>
> OS: Debian testing
> Container: tomcat 4
> Xindice: 1.1b4
>
> Any insights into what is going wrong would be very much appreciated.
>
> Regards
>
> Geoff Shuetrim
>