title: How to include "encoding="UTF-8"" in xml string in Java?
body: <p>I'm reading a <code>customers</code> object through XStream and then replacing any tags I don't need with blanks. Since the <code>customers</code> contain multiple <code>customer</code> it contains the xml form of them. The last <code>.replaceAll("\\&lt;\\?xml(.+?)\\?\\&gt;", "")</code> gets rid of excess <code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;</code> inside the <code>customers</code> string. </p>
  
  <p>I've tried finding <code>&lt;?xml version="1.0"?&gt;</code> then replacing it with <code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;</code> but it caused no change. The final input starts with this:</p>
  
  <p><code>&lt;?xml version="1.0"?&gt;</code></p>
  
  <p>But I want to include the <code>encoding="UTF-8"</code> </p>
  
  <p>How would include that part after I clean up the tags?</p>
  
  <p>Here is the relevant code for the XML and writing it to a file.</p>
  
  <pre><code>    //Final XML string of customers
      String xml = xstream.toXML(customers);
  
      //Remove regex and excess tags
      xml = String.format(xml.replaceAll("&lt;string&gt;", "")
          .replaceAll("&lt;/string&gt;", "")
          .replaceAll("&lt;customers&gt;", "")
          .replaceAll("&lt;/customers&gt;", "")
          .replaceAll("&amp;lt;", "&lt;")
          .replaceAll("&amp;#13;", "")
          .replaceAll("&amp;gt;", "&gt;")
          .replaceAll("\\&lt;\\?xml(.+?)\\?\\&gt;", ""), 4);
  
      System.out.println(xml);
  
      //Create file of grouped XML in a sub-folder
      String timeStamp = new SimpleDateFormat("MM dd yyyy hh.mm.ss a").format(new Date());
      FileWriter fw = new FileWriter("XML Claims\\All Customers" + " " + timeStamp + ".xml");
      fw.write(xml);
      fw.close();
  </code></pre>
  
  <p>Customers object:</p>
  
  <pre><code>public class Customers {
      //this is a string version of customer object converted through xstream
      public ArrayList&lt;String&gt; customers = new ArrayList&lt;String&gt;();
      public String XMLCreationDate = null;
      public int totalNumberOfRecords = 0;
  }
  </code></pre>
  
  <p>Customer object: </p>
  
  <pre><code>public class Customer {
      public LetterContent letterContent = null;
      public LetterIdentifierInformation letterIdentifierInformation = null;
      public Addressee addressee = null;
  }
  </code></pre>
  
  <p>Current XML: </p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;Customers&gt;
       &lt;Customer&gt;
            &lt;letterContent&gt;&lt;/letterContent&gt;
            &lt;addressee&gt;&lt;/address&gt;
       &lt;/Customer&gt;
  &lt;/Customers&gt;
  </code></pre>
  
  <p>Desired XML: </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Customers&gt;
       &lt;Customer&gt;
            &lt;letterContent&gt;&lt;/letterContent&gt;
            &lt;addressee&gt;&lt;/address&gt;
       &lt;/Customer&gt;
  &lt;/Customers&gt;
  </code></pre>