Adding name space to the already created XML document

iCrumble

New Member
I am creating w3c Document object using a String value . Once i created the Document object i wants to add a namespace to the root element of the document.\[code\]Document document = builder.parse(new InputSource(new StringReader(xmlString)));document.getDocumentElement().setAttributeNS("http://com", "xmlns:ns2", "Test");document.setPrefix("ns2");TransformerFactory tranFactory = TransformerFactory.newInstance();Transformer aTransformer = tranFactory.newTransformer();Source src = http://stackoverflow.com/questions/11133754/new DOMSource(document);Result dest = new StreamResult(new File("c:\\xmlFileName.xml"));aTransformer.transform(src, dest);\[/code\]So my input xmlString is \[code\]<product><arg0>DDDDDD</arg0><arg1>DDDD</arg1></product>\[/code\] and i wants the output as \[code\]<ns2:product xmlns:ns2=\"http://com/\"><arg0>DDDDDD</arg0><arg1>DDDD</arg1></ns2:product>\[/code\]I need to add the prefix value and namespace also to the input xml string . If i tried with the above code i am getting exception \[code\]NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.\[/code\]Please help me.
 
Back
Top