title: What's the right way to produce a XML content in Java?
body: <p>I've read several questions and tutorials over the internet such as</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/373833/best-xml-parser-for-java">Best XML parser for Java [closed]</a></li>
  <li><a href="https://stackoverflow.com/questions/27031476/java-xml-how-do-i-get-specific-elements-in-an-xml-node">JAVA XML - How do I get specific elements in an XML Node?</a></li>
  <li><a href="https://stackoverflow.com/questions/22023460/what-is-the-best-way-to-create-xml-files-in-java">What is the best way to create XML files in Java?</a></li>
  <li><a href="https://stackoverflow.com/questions/7646607/how-to-modify-xml-tag-specific-value-in-java">how to modify xml tag specific value in java?</a></li>
  <li><a href="https://docs.oracle.com/javase/tutorial/jaxp/stax/using.html" rel="nofollow noreferrer">Using StAX - From Oracle Tutorials</a></li>
  <li><a href="http://xerces.apache.org/xerces2-j/" rel="nofollow noreferrer">Apache Xerces Docs</a></li>
  <li><a href="http://totheriver.com/learn/xml/xmltutorial.html" rel="nofollow noreferrer">Introduction to XML and XML With Java</a></li>
  <li><a href="http://www.tutorialspoint.com/java_xml/java_dom_modify_document.htm" rel="nofollow noreferrer">Java DOM Parser - Modify XML Document</a></li>
  </ul>
  
  <p>But since this is the very first time I have to manipulate XML documents in Java I'm still a little bit confused. The XML content is written with String concatenation and that seems to me wrong. It is the same to concatenate Strings to produce a JSON object instead of using a JSONObject class. That's the way the code is written right now:</p>
  
  <pre><code>"&lt;msg:restenv xmlns:msg=\"http://www.b2wdigital.com/umb/NEXM_processa_nf_xml_req\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.b2wdigital.com/umb/NEXM_processa_nf_xml_req req.xsd\"&gt;&lt;autenticacao&gt;&lt;usuario&gt;"
              + usuario + "&lt;/usuario&gt;&lt;senha&gt;" + StringUtils.defaultIfBlank(UmbrellaRestClient.PARAMETROS_INFRA_UMBRELLA.get("SENHA_UMBRELLA"), "WS.INTEGRADOR")
              + "&lt;/senha&gt;&lt;/autenticacao&gt;&lt;parametros&gt;&lt;parametro&gt;&lt;p_vl_gnre&gt;" + valorGNRE + "&lt;/p_vl_gnre&gt;&lt;p_cnpj_destinatario&gt;" + cnpjFilial + "&lt;/p_cnpj_destinatario&gt;&lt;p_num_ped_compra&gt;" + idPedido
              + "&lt;/p_num_ped_compra&gt;&lt;p_xml_sefaz&gt;&lt;![CDATA[" + arquivoNfe + "]]&gt;&lt;/p_xml_sefaz&gt;&lt;/parametro&gt;&lt;/parametros&gt;&lt;/msg:restenv&gt;"
  </code></pre>
  
  <p>In my research I think that almost everything I've read pointed to SAX as the best solution but I never really found something really useful to read about it, almost everything states that we have to create a handler and override startElement, endElement and characters methods.</p>
  
  <p>I don't have to serialize the XML file in hard disk or database or anything else, I just need to pass its content to a webservice.</p>
  
  <p>So my question really is, which is the right way to do it?</p>
  
  <ul>
  <li>Concatenate Strings the way things are done right now?</li>
  <li>Write the XML file using a Java API like Xerces? I have no clue on how that can be done.</li>
  <li><p>Read the XML file with streams and just change node texts? My XML without the files would be like that:</p>
  
  <pre><code>&lt;msg:restenv xmlns:msg="{url}"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="{schemaLocation}"&gt;
      &lt;autenticacao&gt;
          &lt;usuario&gt;&lt;/usuario&gt;
          &lt;senha&gt;&lt;/senha&gt;
      &lt;/autenticacao&gt;
      &lt;parametros&gt;
          &lt;parametro&gt;
              &lt;p_vl_gnre&gt;&lt;/p_vl_gnre&gt;
              &lt;p_cnpj_destinatario&gt;&lt;/p_cnpj_destinatario&gt;
              &lt;p_num_ped_compra&gt;&lt;/p_num_ped_compra&gt;
              &lt;p_xml_sefaz&gt;&lt;![CDATA[]]&gt;&lt;/p_xml_sefaz&gt;
          &lt;/parametro&gt;
      &lt;/parametros&gt;
  &lt;/msg:restenv&gt;
  </code></pre></li>
  <li><p>I've also read something about using Apache Velocity as a template Engine since I don't actually have to serialize the XML and that's a approach that I really like because I've already worked with this framework and it's a really simple framework.</p></li>
  </ul>
  
  <p>Again, I'm not looking for the best way, but the right one with tutorials and examples, if possible, on how to get things done.</p>