Wednesday, November 7, 2012

Remove namespace from xml

Sometime we might need to remove all the namespaces from an xml document.

Here is piece of java code which does that job !!


public static String removeNamespaces(String s)
 {
     Transformer transformer = null;
     try
     {
         transformer = templates.newTransformer();
     }
     catch(TransformerConfigurationException transformerconfigurationexception)
     {
         System.err.println("Error transforming XML");
         transformerconfigurationexception.printStackTrace();
         return null;
     }
     StringReader stringreader = new StringReader(s);
     StringWriter stringwriter = new StringWriter();
     try
     {
         transformer.transform(new StreamSource(stringreader), new StreamResult(stringwriter));
         return stringwriter.toString();
     }
     catch(TransformerException transformerexception)
     {
         System.err.println("Error transforming XML");
         transformerexception.printStackTrace();
         return null;
     }
 }

No comments:

Post a Comment