title: Inner Text and child elements
body: <p>I would like to deserialize XML like the following using JAXB in Java:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;root&gt;
      &lt;container&gt;
          inner text that I need
          &lt;foo attrib="meh"&gt;
              &lt;bar&gt;value&lt;/bar&gt;
          &lt;/foo&gt;
      &lt;/container&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>The thing that is tripping me up is capturing the inner text of <code>&lt;container&gt;</code>: I can't use both an <code>@XmlValue</code> to get the inner text and <code>@XmlElement</code> to grab foo elements that come after the inner text. See below for an outline of what I am looking to do</p>
  
  <pre><code>import java.io.ByteArrayInputStream;
  import java.io.UnsupportedEncodingException;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import javax.xml.bind.JAXBContext;
  import javax.xml.bind.JAXBException;
  import javax.xml.bind.Unmarshaller;
  import javax.xml.bind.annotation.XmlAttribute;
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlRootElement;
  import javax.xml.bind.annotation.XmlValue;
  import javax.xml.stream.XMLEventReader;
  import javax.xml.stream.XMLInputFactory;
  import javax.xml.stream.XMLStreamException;
  import javax.xml.stream.events.XMLEvent;
  
  public class App {
  
      private static final String xml = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;&lt;root&gt;&lt;container&gt;text&lt;foo attrib=\"meh\"&gt;&lt;bar&gt;waffles&lt;/bar&gt;&lt;/foo&gt;&lt;/container&gt;&lt;/root&gt;";
  
      @XmlRootElement(name = "foo") static class Foo {
          @XmlAttribute public String attrib;
          @XmlElement   public String bar;
      }
  
      @XmlRootElement(name = "container") static class Container {
          //@XmlValue   public String innerText;
          @XmlElement public Foo foo;
      }
  
      public static void main(String[] args) {
          try {
              final XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
              final XMLEventReader xer = xmlInputFactory.createXMLEventReader(new ByteArrayInputStream(xml.getBytes("UTF-8")));
  
              XMLEvent evt = xer.nextEvent(); // start document
              evt = xer.nextEvent(); // &lt;root&gt;
              evt = xer.peek(); // advance to &lt;container&gt;
  
              JAXBContext ctx = JAXBContext.newInstance(Container.class);
              Unmarshaller um = ctx.createUnmarshaller();
              Object o = um.unmarshal(xer);
          } catch (UnsupportedEncodingException ex) {
              Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
          } catch (XMLStreamException ex) {
              Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
          } catch (JAXBException ex) {
              Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  }
  </code></pre>