It was a combination of things that stopped me from adding a default namespace.
The solution as with most things in IT is straight forward but not easy getting there:
The key here is using the .addNamespaceDeclaration(“”, namespace) with the first param set to “” this is not intuitive I was using .setNamespace() which doesn’t work.
Also I didn’t realise that creating an element using:
new Element("env:CEnvelope", IConstants.ENV_NAMESPACE);
automatically creates a namespace xmlns:env in the root element; I was explicitly doing this again; this doesn’t cause an issue but if you use .addNamespaceDeclaration(“”, namespace) after this it doesn’t work.
The line with ** is all you need:
private void setRoot(){
Element root = new Element("env:CEnvelope", IConstants.ENV_NAMESPACE);
//Add root Element to the Document
this.doc = new Document(root);
root.addNamespaceDeclaration("xsi", IConstants.XSI_NAMESPACE);
**root.addNamespaceDeclaration("", IConstants.PAT_NAMESPACE);
//root.setNamespaceURI(IConstants.PAT_NAMESPACE);
//root.addNamespaceDeclaration("env", IConstants.ENV_NAMESPACE);
Oh yes, I didn't need the inv namespace as this is the same as the default namespace!
Cheers :-)