You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by jo...@xml4pharma.com on 2005/02/01 20:28:02 UTC

changing DOM with DTD declaration to DOM with default namespace

Dear all,

I want to convert an XML document containing a DOCTYPE declaration into
one with a default namespace (i.e. the root element should become <ODM
xmlns="http://www.cdisc.org/ns/odm/v1.2" ...>

So, what I do is:
DocumentBuilderFactory docBuilderFactory =
DocumentBuilderFactory.newInstance();
docBuilderFactory.setNamespaceAware(true);
Document doc = null;
try {
  DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
  doc = docBuilder.parse(xmlfile);  // xmlfile is the input XML file
} catch ...
}

doc.removeChild(doc.getDoctype());  // remove the DTD declaration
doc.getDocumentElement().setAttribute("xmlns","http://www.cdisc.org/ns/odm/v1.2");

The last statement is known not to be correct. 
Maybe I could use: doc.createElementNS(...) to create a new root element
and then add all the attributes and child nodes of the original
document to the new element.
But isn't there a better and faster way ?

Many thanks in advance

Jozef Aerts

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org