XML : How to create tag with namespace prefix

I am trying to add a prefix to a tag to represent a particular namespace - as can be seen below

  String envelopePrefix = "omgEnv";              String businessPrefix = "omgBS";              String namespaceURI = "http://www.w3.org/2000/xmlns/";                DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();              DocumentBuilder docBuilder = docFactory.newDocumentBuilder();                Document doc = docBuilder.newDocument();              Element rootElement = doc.createElement("OmgeoMessageContainer");                rootElement.setAttributeNS(namespaceURI, "xmlns:" + envelopePrefix, "http://www.omgeo.com/schema/v1.0/envelope");              rootElement.setAttributeNS(namespaceURI, "xmlns:" + businessPrefix, "http://www.omgeo.com/schema/v1.0/BusinessServices");                doc.appendChild(rootElement);                Element messageParties = doc.createElementNS(namespaceURI, envelopePrefix + ":MessageParties");              rootElement.appendChild(messageParties);    

Unfortunately my messageParties element is failing with the following error -

org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.

How are you supposed to prefix a tag with the correct namespace definition? Event the setPrefix method throws the same error.

Thanks

No comments:

Post a Comment