title: Return an XML formatted result from Web service Java
body: <p>I have this simple formatter class in my webservice</p>
  
  <pre><code>public class SimpleFormatter extends BaseFormatter
  {
  
      public String getXml(String str)
      {
          sb = new StringBuilder();
          sb.append(XML);
          sb.append(str);
          return sb.toString();
      }
  
      public String getXml(byte[] ba)
      {
          sb = new StringBuilder();
          sb.append(XML);
          sb.append(new String(ba));
          return sb.toString();
      }
  
      public static void main(String[] args)
      {
          SimpleFormatter sf = new SimpleFormatter();
          String result = sf.getXml("&lt;result&gt;&lt;/result&gt;");
          System.out.println(result);
      }
  </code></pre>
  
  <p><strong>BaseFormatter Class</strong></p>
  
  <pre><code> public abstract class BaseFormatter
  {
  
      protected static final String XML = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?&gt;";
      protected static final String ERROR = "error";
      protected static final String RESULT = "result";
  
      public static final String EMPTY = XML + "&lt;results/&gt;";
  
      protected StringBuilder sb = null;  
  
      protected void openTag(String tag)
      {
          sb.append("&lt;");
          sb.append(tag);
          sb.append("&gt;");
      }
  
      protected void openTagWithAttr(String tag, String attr, String val)
      {
          sb.append("&lt;");
          sb.append(tag);
          if (val.length()&gt;0)
          {
              sb.append(" ");
              sb.append(attr);
              sb.append("=\"");
              sb.append(val);
              sb.append("\"");
          }
          sb.append("&gt;");
      }
  
      protected void closeTag(String tag)
      {
          sb.append("&lt;/");
          sb.append(tag);
          sb.append("&gt;");
      }
  
  /**
  Returns an error document
  **/
  
      public String getError(String msg)
      {
          sb = new StringBuilder();
          sb.append(XML);
          openTag(ERROR);
          sb.append(msg);
          closeTag(ERROR);
          return sb.toString();
      }
  
      private void addEmpty(String tag)
      {
          sb.append("&lt;");
          sb.append(tag);
          sb.append("/&gt;");
      }
  
  /**
  Adds elements to the document; NOTE: adds an empty element even if the value list is empty
  **/
  
      protected void addElement(List&lt;String&gt; vals, List&lt;String&gt; keys)
      {
          int len = vals.size();
          openTag(RESULT);
          if (len == 0 &amp;&amp; keys.size() &gt; 0) addEmpty(keys.get(0));
          for (int i=0; i &lt; len; i++)
          {
              String key = keys.get(i);
              openTag(key);
              sb.append(vals.get(i));
              closeTag(key);
          }
          closeTag(RESULT);
      }
  </code></pre>
  
  <p>}
  And im using it to format result when using the web service</p>
  
  <pre><code> private SimpleFormatter formatter = null;
  
  
        public String getJobData(Map&lt;String, String&gt; paramMap)
    {
      String str1 = (String)paramMap.get("jobId");
      if (str1 == null) {
        return this.formatter.getError("No job ID provided for query");
      }
      Query_filter localQuery_filter = new Query_filter("Job", str1);
      Query_filter[] arrayOfQuery_filter = { localQuery_filter };
      Node localNode = mgr.getQueryResult("HolderJob", JOB_NAMES, JOB_TAGS, arrayOfQuery_filter);
      if (localNode == null) {
        return this.formatter.getError("Could not retrieve job data; check job ID");
      }
      String str2 = null;
      try
      {
        str2 = Documents.writeToString(localNode);
      }
      catch (Exception localException) {}
      return this.formatter.getXml(str2);
    }
  </code></pre>
  
  <p>I am getting a null pointer exception when using the formatter  class. </p>
  
  <p>but if i do it like this:</p>
  
  <pre><code>return str2;
  </code></pre>
  
  <p>the result im getting is just a plain string:</p>
  
  <p><strong>false150305510190 ROW VERI219254591150110</strong></p>
  
  <p>This is the result i am expecting if i use the formatter class.</p>
  
  <pre><code>&lt;Table&gt;&lt;Claimable&gt;false&lt;/Claimable&gt;&lt;MinorRev&gt;150305&lt;/MinorRev&gt;&lt;Operation&gt;510190 ROW VERI&lt;/Operation&gt;&lt;Experiment&gt;219254&lt;/Experiment&gt;&lt;HTNum&gt;591&lt;/HTNum&gt;&lt;WaferEC&gt;150110&lt;/WaferEC&gt;&lt;/Table&gt;
  </code></pre>
  
  <p>Im using Netbeans 8.0.2</p>
  
  <p>Apache 7.0.63</p>
  
  <p>Can anyone guide me about this? </p>
  
  <p>Thank you.</p>