Hi,
I am trying to import one document into another and have the imported document asume the namespace of the parent (importing) document.
I am trying to import a series of XML elements from a file, these come with no namespace definition. However I want to read these and add them to a parent document. The parent document comes with a namespace, and I want the imported elements to have the same namespace as the parent.
Document
doc = .... parent doc.
Element ted = doc.createElementNS("http://www.sii.cl/SiiDte","TED");
Node caf = doc.importNode(cafDoc.getDocumentElement(), true);
ted.appendChild(caf);
Now when I import the element "CAF" (the caf node declared above) it appears in the document under the default namespace, ie:- <CAF xmlns="">, obviously I want it to appear as <CAF> because the parent document has the namespace definition "http://www.sii.cl/SiiDte" in its root element.
I also tried to create my own CAF element and directly copy the attributes and elements from my CAF document manually, as below...
m_cafRootNode = cafDoc.createElementNS(SII_NAMESPACE, "CAF");
for(int i = 0;
i < cafDoc.getDocumentElement().getAttributes().getLength(); i++)
{ m_cafRootNode.appendChild(cafDoc.getDocumentElement().getAttributes().item(i));
}
for(int i = 0;
i < cafDoc.getDocumentElement().getChildNodes().getLength(); i++)
{ m_cafRootNode.appendChild(cafDoc.getDocumentElement().getChildNodes().item(i));
}
However this gives an error upon insertion of the attributes, as shown below
org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted.
Anyone have any ideas, thanks in advance, Jonathan.