title: Parsing with SAX and handling character entities
body: <p>I am parsing a MathML expression with SAX (although the fact that it's MathML may not be completely relevant).  An example input string is </p>
  
  <pre><code>&lt;math xmlns='http://www.w3.org/1998/Math/MathML'&gt;
       &lt;mrow&gt;
            &lt;mo&gt;&amp;lambda;&lt;/mo&gt;
       &lt;/mrow&gt;
  &lt;/math&gt;
  </code></pre>
  
  <p>In order for the SAX parser to accept this string, I expand it a bit:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
       &lt;!DOCTYPE doc_type [
            &lt;!ENTITY nbsp "&amp;#160;"&gt;
            &lt;!ENTITY amp "&amp;#38;"&gt;
  ]&gt;
  &lt;body&gt;
       &lt;math xmlns='http://www.w3.org/1998/Math/MathML'&gt;
            &lt;mrow&gt;
                 &lt;mo&gt;&amp;lambda;&lt;/mo&gt;
            &lt;mrow&gt;
       &lt;/math&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>Now, when I run the SAX parser on this, I get an exception:</p>
  
  <pre><code>[Fatal Error] :5:86: The entity "lambda" was referenced, but not declared.
  org.xml.sax.SAXParseException: The entity "lambda" was referenced, but not 
                                 declared.
      at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
  </code></pre>
  
  <p>However, I know how to fix that.  I simply add this line to the string being parsed:</p>
  
  <pre><code>        &lt;!ENTITY lambda "&amp;#923;"&gt;
  </code></pre>
  
  <p>This gives me</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
       &lt;!DOCTYPE doc_type [
            &lt;!ENTITY nbsp "&amp;#160;"&gt;
            &lt;!ENTITY amp "&amp;#38;"&gt;
            &lt;!ENTITY lambda "&amp;#923;"&gt;
  ]&gt;
  &lt;body&gt;
       &lt;math xmlns='http://www.w3.org/1998/Math/MathML'&gt;
            &lt;mrow&gt;
                 &lt;mo&gt;&amp;lambda;&lt;/mo&gt;
            &lt;mrow&gt;
       &lt;/math&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>Now, it parses just fine, thank you.</p>
  
  <p>However, the problem is that I can't add an ENTITY declaration for every possible character entity that might be used in MathML (for example, "part", "notin", and "sum").  </p>
  
  <p>How do I rewrite this string so that it can be parsed for any possible character entity that might be included?</p>