1 Reply Latest reply: Jan 17, 2007 1:34 AM by 807607 RSS

    remove namespace from element name in dom document

    807607
      hello,
      which is the most efficient way to remove the namespace from the name of all the elements of a dom document ?
      eg:
      ...
      <ns1:mytag>aaa</ns1:mytag> should become <mytag>aaa</mytag>

      is it possible to have an example ?


      thanks
        • 1. Re: remove namespace from element name in dom document
          807607
          A simple way is like this,

          may be it helps u,


          public class NamePrefixHandler
          {
               private WebApplicationContext wc;
               private String replacedNewString = null;
               private String newLine = "\r\n";
               
               
               public String removeNamePrefix(String xmlString)
               {     
                    StringBuffer sb = new StringBuffer();
                    
                    if ((xmlString != null) && (xmlString.trim().length() > 0))
                    {     
                         try
                         {
                              ByteArrayInputStream bis = new ByteArrayInputStream(xmlString.getBytes());
                              InputStreamReader input = new InputStreamReader(bis);
                              BufferedReader reader = new BufferedReader(input);
                              String line = null;
                              
                              while ((line = reader.readLine()) != null)
                              {
                                   replacedNewString = replace(line);
                                   sb.append(replacedNewString).append(newLine);
                              }
                                             
                         }
                         catch (Exception ee)
                         {
                    
                              return null;
                         }
                    }
                    
                    return sb.toString();
               }
               
                    
               public String replace(String s)
               {
                    String replacedString = s;
                    String finalReplacedString = null;
                    
                    try
                    {               
                         int startIndex = 0, endIndex = 0;
                         
                         if ((s.toLowerCase()).contains("xyz:"))
                         {
                              startIndex = s.indexOf("<xyz");
                    endIndex = s.indexOf(":", startIndex);

                    if ((startIndex > -1) && (endIndex > -1) && (endIndex > startIndex))
                    {
                         replacedString = s.substring(0, startIndex);
                         replacedString += "<";
                         replacedString += s.substring(endIndex + 1);
                    }
                         }
                         
                         // Checking for xyz: at the end of the tag in the replaced string
                         
                         finalReplacedString = replacedString;
                         
                         if ((replacedString.toLowerCase()).contains("xyz:"))
                         {
                              startIndex = replacedString.indexOf("</xyz");
                    endIndex = replacedString.indexOf(":", startIndex);

                    if ((startIndex > -1) && (endIndex > -1) && (endIndex > startIndex))
                    {
                         finalReplacedString = replacedString.substring(0, startIndex);
                         finalReplacedString += "</";
                         finalReplacedString += replacedString.substring(endIndex + 1);
                    }
                         }
                    }
                    catch (Exception ee)
                    {
                         return null;
                    }
                    
                    finalReplacedString.trim();
                    
                    return finalReplacedString;
               }     
          }



          ----------------------------------

          here i had removed <xyz:> and </xyz:> from the xml