title: Java Web Service returns string with &gt; and &lt; instead of > and <
body: <p>I have a java web service that returns a string. I'm creating the body of this xml string with a <code>DocumentBuilder</code> and <code>Document</code> class. When I view source of the returned XML (Which looks fine in the browser window) instead of &lt;> it's returning <code>&amp;lt;</code> and <code>&amp;gt;</code> around the XML nodes.</p>
  
  <p>Please help.</p>
  
  <p><b>****UPDATE (including code example)</b><br>
  The code is not including any error catching, it was stripped for simplicity.
  One code block and three methods are included:
  The first code block (EXAMPLE SETUP) shows the basics of what the Document object is setup to be like. the method <code>appendPayment(...)</code> is where the actual document building happens. It calls on two helper methods <code>getTagValue(...)</code> and <code>prepareElement(...)</code><br>
  **Note, this code is meant to copy specific parts from a pre-existing xml string, <code>xmlString</code>, and grap the necessary information to be returned later.</p>
  
  <p><b>****UPDATE 2</b>
  Response added at the end of the question</p>
  
  <p><b>************ Follow-up question to the first answer is here:</b><br>
  <a href="https://stackoverflow.com/questions/7877515/how-to-return-arbitrary-xml-document-using-an-eclipse-axis2-pojo-service">How to return arbitrary XML Document using an Eclipse/AXIS2 POJO Service</a></p>
  
  <pre><code>EXAMPLE SETUP
  {
      //create new document
      DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder newDocBuilder = docFactory.newDocumentBuilder();
      Document newDoc = newDocBuilder.newDocument();
      Element rootElement = newDoc.createElement("AllTransactions");
  
      newDoc.appendChild(rootElement);
      appendPayment(stringXML, newDoc);
  }
  
  public static void appendPayment(String xmlString, Document newDoc) throws Exception
  {
      //convert string to inputstream
      ByteArrayInputStream bais = new ByteArrayInputStream(xmlString.getBytes());
      DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
      Document oldDoc =   docBuilder.parse(bais);
      oldDoc.getDocumentElement().normalize();            
  
      NodeList nList = oldDoc.getChildNodes();
      Node nNode = nList.item(0);
      Element eElement = (Element) nNode;
  
      //Create new child node for this payment
      Element transaction = newDoc.createElement("Transaction");
      newDoc.getDocumentElement().appendChild(transaction);
  
  
      //status
      transaction.appendChild(prepareElement("status", eElement, newDoc));
  
      //amount
      transaction.appendChild(prepareElement("amount", eElement, newDoc));
  }
  
  private static String getTagValue(String sTag, Element eElement)
  {
      NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
      Node nValue = (Node) nlList.item(0);
      return nValue.getNodeValue();
  }
  
  private static Element prepareElement(String sTag, Element eElement, Document newDoc)
  {
      String str = getTagValue(sTag, eElement);
      Element newElement = newDoc.createElement(sTag);
      newElement.appendChild(newDoc.createTextNode(str));
      return newElement;
  }
  </code></pre>
  
  <p>Finally, I use the following method to convert the final <code>Document</code> object to a <code>String</code></p>
  
  <pre><code>public static String getStringFromDocument(Document doc)
  {
      try
      {
         DOMSource domSource = new DOMSource(doc);
         StringWriter writer = new StringWriter();
         StreamResult result = new StreamResult(writer);
         TransformerFactory tf = TransformerFactory.newInstance();
         Transformer transformer = tf.newTransformer();
         transformer.transform(domSource, result);
         return writer.toString();
      }
      catch(TransformerException ex)
      {
         ex.printStackTrace();
         return null;
      }
  }
  </code></pre>
  
  <p>The header type of the response is as follows</p>
  
  <pre><code>Server: Apache-Coyote/1.1
  Content-Type: text/xml;charset=utf-8
  Transfer-Encoding: chunked
  </code></pre>
  
  <p>This is an example response</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
      &lt;soapenv:Body&gt;
          &lt;getTransactionsResponse xmlns="http://services.paypal.com"&gt;
          &lt;getTransactionsReturn&gt;&amp;lt;AllTransactions&amp;gt;&amp;lt;Transaction&amp;gt;&amp;lt;status&amp;gt;PENDING&amp;lt;/status&amp;gt;&amp;lt;amount&amp;gt;55.55&amp;lt;/amount&amp;gt;&amp;lt;/transaction&amp;gt;
          &lt;/getTransactionsResponse&gt;
      &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;
  </code></pre>