• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Importing a node into a different namespace

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic