I am trying to merge two XML documents both with standard Oracle JDK 7 and also with Saxon HE but I keep getting an:
org.w3c.dom.DOMException: NOT_SUPPORTED_ERR: The implementation does not support the requested type of object or operation.`
On the line with the importNode
below (same thing happens with adoptNode
BTW):
import java.io.*; import org.w3c.dom.*; import javax.xml.parsers.*; public class FooMain { private static Document slurp(String s) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); return factory.newDocumentBuilder().parse(new ByteArrayInputStream(s.getBytes("UTF-8"))); } public static void main(String args[]) throws Exception { Document doc = slurp("<a></a>"); Document doc2 = slurp("<b></b>"); Node not_used = doc.importNode(doc2, true); } }
I’ve tried both with Saxon-HE-9.4.jar
on my classpath or not, but I still get the same error.
update
Based on comments I received, when I try with:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance("net.sf.saxon.dom.DocumentBuilderFactoryImpl", null);
I get instead:
java.lang.UnsupportedOperationException: The Saxon DOM implementation cannot be updated at net.sf.saxon.dom.NodeOverNodeInfo.disallowUpdate(NodeOverNodeInfo.java:719) at net.sf.saxon.dom.DocumentOverNodeInfo.importNode(DocumentOverNodeInfo.java:211) at FooMain.main(FooMain.java:16)
After googling I see old messages to the effect that Saxon apparently creates a read-only DOM which is weird since I thought that one of the comparison points of DOM vs SAX is that DOM is read-write whereas SAX is read-only.
Advertisement
Answer
To merge two documents (I’m not sure exactly what you mean by this, but I’ll guess) using Saxon, you don’t want to be fiddling about with a DOM. Just run this XQuery:
<doc>{doc('a.xml'), doc('b.xml')}</doc>