title: Is there a Java XML API that can parse a document without resolving character entities?
body: <p>I have program that needs to parse XML that contains character entities.  The program itself doesn't need to have them resolved, and the list of them is large and will change, so I want to avoid explicit support for these entities if I can.</p>
  
  <p>Here's a simple example:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xml&gt;Hello there &amp;something;&lt;/xml&gt;
  </code></pre>
  
  <p>Is there a Java XML API that can parse a document successfully without resolving (non-standard) character entities?  Ideally it would translate them into a special event or object that could be handled specially, but I'd settle for an option that would silently suppress them.</p>
  
  <p><strong>Answer &amp; Example:</strong></p>
  
  <p>Skaffman gave me the answer: use a StAX parser with <code>IS_REPLACING_ENTITY_REFERENCES</code> set to false.</p>
  
  <p>Here's the code I whipped up to try it out:</p>
  
  <pre><code>XMLInputFactory inputFactory = XMLInputFactory.newInstance();
  inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
  XMLEventReader reader = inputFactory.createXMLEventReader(
      new FileInputStream("your file here"));
  
  while (reader.hasNext()) {
      XMLEvent event = reader.nextEvent();
      if (event.isEntityReference()) {
          EntityReference ref = (EntityReference) event;
          System.out.println("Entity Reference: " + ref.getName());
      }
  }
  </code></pre>
  
  <p>For the above XML, it will print "<code>Entity Reference: something</code>".</p>