title: Java - Read XML and leave all entities alone
body: <p>I want to read XHTML files using SAX or StAX, whatever works best.
  But I don't want entities to be resolved, replaced or anything like that.
  Ideally they should just remain as they are.
  I don't want to use DTDs.</p>
  
  <p>Here's an (executable, using Scala 2.8.x) example:</p>
  
  <pre><code>import javax.xml.stream._
  import javax.xml.stream.events._
  import java.io._
  
  println("StAX Test - "+args(0)+"\n")
  val factory = XMLInputFactory.newInstance
  factory.setProperty(XMLInputFactory.SUPPORT_DTD, false)
  factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false)
  
  println("------")
  val xer = factory.createXMLEventReader(new FileReader(args(0)))
  val entities = new collection.mutable.ArrayBuffer[String]
  while (xer.hasNext) {
      val event = xer.nextEvent
      if (event.isCharacters) {
          print(event.asCharacters.getData)
      } else if (event.getEventType == XMLStreamConstants.ENTITY_REFERENCE) {
          entities += event.asInstanceOf[EntityReference].getName
      }
  }
  println("------")
  println("Entities: " + entities.mkString(", "))
  </code></pre>
  
  <p>Given the following xhtml file ...</p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;StAX Test&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;Hallo StAX&lt;/h1&gt;
          &lt;p id="html"&gt;
              &amp;lt;div class=&amp;quot;header&amp;quot;&amp;gt;
          &lt;/p&gt;
          &lt;p id="stuff"&gt;
              &amp;Uuml;berdies sollte das hier auch als Copyright sichtbar sein: &amp;#169;
          &lt;/p&gt;
          Das war's!
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>... running <code>scala stax-test.scala stax-test.xhtml</code> will result in:</p>
  
  <pre><code>StAX Test - stax-test.xhtml
  
  ------
  
  
      StAX Test
  
  
      Hallo StAX
  
        &lt;div class="header"&gt;
  
  
        berdies sollte das hier auch als Copyright sichtbar sein: ?
  
      Das war's!
  
  ------
  Entities: Uuml
  </code></pre>
  
  <p>So all entities have been replaced more or less sucessfully.
  What I would have expected and what I want is this, though:</p>
  
  <pre><code>StAX Test - stax-test.xhtml
  
  ------
  
  
      StAX Test
  
  
      Hallo StAX
  
        &amp;lt;div class=&amp;quot;header&amp;quot;&amp;gt;
  
  
        &amp;Uuml;berdies sollte das hier auch als Copyright sichtbar sein: &amp;#169;
  
      Das war's!
  
  ------
  Entities: // well, or no entities above and instead:
  // Entities: lt, quot, quot, gt, Uuml, #169
  </code></pre>
  
  <p>Is this even possible?
  I want to parse XHTML, do some modifications and then output it like that as XHTML again. So I really want the entities to remain in the result.</p>
  
  <p>Also I don't get why Uuml is reported as an EntityReference event while the rest aren't.</p>