title: Unable to parse value containing special character? Using sax parser
body: <p>I am new to parsing field. I'm trying to write a parser code but unable to get the value with respect to a particular tag that value contains <code>ampersand(&amp;)</code>. Please help me to get the solution.</p>
  
  <h3>My <code>xml</code> file looks like</h3>
  
  <pre><code>&lt;system&gt;
  &lt;u_id&gt;10145&lt;/u_id&gt;
  &lt;serial_no&gt;1800015&lt;/serial_no&gt;
  &lt;branch_name&gt;B &amp; P Infotech Ltd.&lt;/branch_name&gt;
  &lt;/system&gt;
  </code></pre>
  
  <p>and I have tried with this java code, but it's not giving me proper output.</p>
  
  <h3>main class</h3>
  
  <pre><code>package com.satya.xmltest;
  
  import javax.xml.parsers.SAXParser;
  import javax.xml.parsers.SAXParserFactory;
  
  public class SaxTest {
  
      public static void main(String[] args) {
          SAXParserFactory parserFactory = SAXParserFactory.newInstance();
          SaxtestHandler handler=new SaxtestHandler();
          try {
              SAXParser parser = parserFactory.newSAXParser();
              parser.parse("C:\\Users\\abc\\Desktop\\test.xml", handler);
          } catch (Exception e) {
          }
          SystemTo systemTo=handler.systemTo;
          System.out.println("Uid :"+systemTo.getUid());
          System.out.println("serial number :"+systemTo.getSerialNumber());
          System.out.println("name :"+systemTo.getName());
      }
  }
  </code></pre>
  
  <h3>Handler class</h3>
  
  <p>In this class the parsing is done and setting the data values to data container class.</p>
  
  <pre><code>package com.satya.xmltest;
  
  import org.xml.sax.Attributes;
  import org.xml.sax.SAXException;
  import org.xml.sax.helpers.DefaultHandler;
  
  public class SaxtestHandler extends DefaultHandler {
      String content = "";
      SystemTo systemTo=new SystemTo();
  
      @Override
      public void startElement(String uri, String localName, String qName,
          Attributes attributes) throws SAXException {
  
          switch (qName) {
              case "system":
                  System.out.println("inside company");
                  break;
          }
      }
  
      @Override
      public void endElement(String uri, String localName, String qName)
          throws SAXException {
          switch (qName) {
              case "u_id":
                  systemTo.setUid(content);
                  break;
              case "serial_no":
                  systemTo.setSerialNumber(content);
                  break;
              case "branch_name":
                  systemTo.setName(content);
                  break;
          }
      }
  
      @Override
      public void characters(char[] ch, int start, int length)
          throws SAXException {
          content = String.copyValueOf(ch, start, length).trim();
      }
  }
  </code></pre>
  
  <h3>Data container class</h3>
  
  <pre><code>package com.satya.xmltest;
  
  public class SystemTo {
  
      private String uid;
      private String serialNumber;
      private String name;
      public String getUid() {
          return uid;
      }
      public void setUid(String uid) {
          this.uid = uid;
      }
      public String getSerialNumber() {
          return serialNumber;
      }
      public void setSerialNumber(String serialNumber) {
          this.serialNumber = serialNumber;
      }
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
  }
  </code></pre>
  
  <h3>My output is:</h3>
  
  <pre><code>Uid: 10145
  serial number: 1800015
  name: null
  </code></pre>
  
  <h3>But I need:</h3>
  
  <pre><code>Uid: 10145
  serial number: 1800015
  name: B &amp; P Infotech Ltd.
  </code></pre>
  
  <p>Thanks in advance.</p>