title: Remove whitespace within XML fields using Java
body: <p>I'm facing an issue with removing whitespaces within the value fields in the xml data.</p>
  
  <p>eg:</p>
  
  <p>Input</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;ns:myOrder xmlns:ns="http://w3schools.com/BusinessDocument" xmlns:ct="http://something.com/CommonTypes"&gt;
    &lt;MessageHeader&gt;
       &lt;ct:ID&gt;i7                           &lt;/ct:ID&gt;
       &lt;ct:ID&gt;i7                           &lt;/ct:ID&gt;
       &lt;ct:ID&gt;i7                           &lt;/ct:ID&gt;
       &lt;ct:ID&gt;i7                           &lt;/ct:ID&gt;
       &lt;ct:Name&gt; Company Name           &lt;/ct:Name&gt;
   &lt;/MessageHeader&gt;
  &lt;/ns:myOrder&gt;
  </code></pre>
  
  <p>Expected output:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
    &lt;ns:myOrder xmlns:ns="http://w3schools.com/BusinessDocument" xmlns:ct="http://something.com/CommonTypes"&gt;
      &lt;MessageHeader&gt;
         &lt;ct:ID&gt;i7&lt;/ct:ID&gt;
         &lt;ct:ID&gt;i7&lt;/ct:ID&gt;
         &lt;ct:ID&gt;i7&lt;/ct:ID&gt;
         &lt;ct:ID&gt;i7&lt;/ct:ID&gt;
         &lt;ct:Name&gt;Company Name&lt;/ct:Name&gt;
      &lt;/MessageHeader&gt;
    &lt;/ns:myOrder&gt;
  </code></pre>
  
  <p>I tried with the below code</p>
  
  <pre><code>public static String getTrimmedXML(String rawXMLFilename) throws Exception
       {
            BufferedReader in = new BufferedReader(new FileReader(rawXMLFilename));
       String str;
       String trimmedXML = null;     
       while ((str = in.readLine()) != null) 
       {
            String str1 = str;
            if (str1.length()&gt;0) 
            {
                 str1 = str1.trim();
                 if(str1.charAt(str1.length()-1) == '&gt;')
                 {
                      trimmedXML = trimmedXML + str.trim();
                 }
                 else
                 {
                      trimmedXML = trimmedXML + str;
                 }
            }
       }     
       in.close();
       return trimmedXML.substring(4);
       }
  </code></pre>
  
  <p>I'm unable to remove those spaces. Please let me know where i'm going wrong</p>
  
  <p>Regards,
  Monish</p>