title: Using Multiple @XmlAnyElement in JAXB to manually handle </> Characters in Multiple Strings
body: <p>We are using JAXB to unmarshal pre-stored XML that can't be changed by us. Unfortunately, the pre-stored XML contains some HTML brackets, <code>&lt; and &gt;, without CDATA.</code>. For instance, we have to unmarshal this with JAXB (note there is no CDATA):</p>
  
  <pre><code>&lt;Article&gt;
    &lt;ArticleTitle&gt;Note the unescaped inner HTML (T&lt;sub&gt;3&lt;/sub&gt;) in the title&lt;/ArticleTitle&gt;
    &lt;Abstract&gt;
       &lt;AbstractText&gt;This is another element that has unescaped HTML: T&lt;sub&gt;3&lt;/sub&gt;. &lt;/AbstractText&gt;
    &lt;/Abstract&gt;
  &lt;/Article&gt;
  </code></pre>
  
  <p>We need to preserve the HTML data above. JAXB chokes on these and gives us incorrect model values.</p>
  
  <p>One solution that works is to use <code>@XmlAnyElement</code> and then implement a <code>DomHandler</code> (see below).</p>
  
  <p><strong>But unfortunately only 1 @XmlAnyElement is allowed per class. If I add several, for Title &amp; Abstract, only one of them works at  time.</strong> So this solution only works for 1 field that has this problem. I've tried implementing an <code>XmlAdapter</code> (not a <code>DomHandler</code>) but the string already comes in truncated so I can't use that solution, only this one.</p>
  
  <p><a href="https://stackoverflow.com/questions/25811351/force-jaxb-unmarshaller-to-ignore-html-tags">Force jaxb unmarshaller to ignore html Tags</a></p>
  
  <pre><code>@XmlAnyElement(value=AbstractTextHandler.class)
  protected String abstractText=null;
  </code></pre>
  
  <p>then implement</p>
  
  <pre><code>public class AbstractTextHandler implements DomHandler&lt;String, StreamResult&gt; {
      private final Logger log = Logger.getLogger(getClass().getName());
      private static final String START_TAG = "&lt;Abstract&gt;";
      private static final String END_TAG = "&lt;/Abstract&gt;";
  
      private StringWriter xmlWriter = new StringWriter(); 
  
      public StreamResult createUnmarshaller(ValidationEventHandler errorHandler) {
          return new StreamResult(xmlWriter);
      }
  
      public String getElement(StreamResult rt) {
          String xml = rt.getWriter().toString();
          int beginIndex = xml.indexOf(START_TAG) + START_TAG.length();
          int endIndex = xml.indexOf(END_TAG);
          String result = xml.substring(beginIndex, endIndex);
          log.info("Processing.." + result);
          return result;
      }
  
      public Source marshal(String n, ValidationEventHandler errorHandler) {
          try {
              String xml = START_TAG + n.trim() + END_TAG;
              StringReader xmlReader = new StringReader(xml);
              return new StreamSource(xmlReader);
          } catch(Exception e) {
              throw new RuntimeException(e);
          }
      }
  }
  </code></pre>
  
  <p>This needs to work for multiple fields, but only 1 <code>@XmlAnyElement</code> is allowed.</p>
  
  <p>Is there a way for us to use a SAX parser or something to handle the XML umarshalling ourselves, to avoid choking on these HTML brackets? Or is there an <code>@XmlAnyElement</code> workaround?</p>