title: XML escape code
body: <p>I have written a method to check my XML strings for &amp;.  </p>
  
  <p>I need to modify the method to include the following:</p>
  
  <p><code>&lt;</code> &amp;lt  </p>
  
  <p><code>&gt;</code> &amp;gt</p>
  
  <p><code>\</code> &amp;guot </p>
  
  <p><code>&amp;</code> &amp;amp </p>
  
  <p><code>\</code> &amp;apos</p>
  
  <p>Here is the method</p>
  
  <pre><code>private String xmlEscape(String s) {
      try {
          return s.replaceAll("&amp;(?!amp;)", "&amp;amp;");
      }
      catch (PatternSyntaxException pse) {
          return s;
      }
  } // end xmlEscape()
  </code></pre>
  
  <p>Here is the way I am using it</p>
  
  <pre><code> sb.append("            &lt;Host&gt;" + xmlEscape(url.getHost()) + "&lt;/Host&gt;\n");
  </code></pre>
  
  <p>How can I modify my method to incorporate the rest of the symbols? </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I think I must not have phrase the question correctly.
  In the <code>xmlEscape()</code> method I am wanting to check the string for the following chars 
  <code>&lt;</code> <code>&gt;</code> <code>'</code> <code>"</code> <code>&amp;</code>, if they are found I want to replace the found char with the correct char.</p>
  
  <p>Example: if there is a char <code>&amp;</code> the char would be replaced with <code>&amp;amp</code>; in the string.</p>
  
  <p>Can you do something as simple as</p>
  
  <pre><code>try {
     s.replaceAll("&amp;(?!amp;)", "&amp;amp;");
     s.replaceAll("&lt;", "&amp;lt;");
     s.replaceAll("&gt;", "&amp;gt;");
     s.replaceAll("'", "&amp;apos;");
     s.replaceAll("\"", "&amp;quot;");
     return s;
  }
  catch (PatternSyntaxException pse) {
     return s;
  }   
  </code></pre>