title: Soap response header element parsing through JAXB
body: <p>I have the following soap response header from which I want to unmarshall the value of the element using JAXB.</p>
  
  <pre><code>&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:com="http://example.com" xmlns:ass="http://example.com"&gt;
      &lt;soapenv:Header&gt;
        &lt;com:HeaderResponse&gt;
           &lt;com:RequestId&gt;1234&lt;/com:RequestId&gt;
        &lt;/com:HeaderResponse&gt;
      &lt;/soapenv:Header&gt;
      &lt;soapenv:Body&gt;
          ......................
      &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p>I have tried many options but not able to get the value of RequestID element. One of the method is:</p>
  
  <pre><code>package com.example.model;
  
  import javax.xml.bind.annotation.XmlAccessType;
  import javax.xml.bind.annotation.XmlAccessorType;
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlRootElement;
  import javax.xml.bind.annotation.XmlType;
  
  @XmlAccessorType(XmlAccessType.FIELD)
  @XmlType(name = "HeaderResponse", propOrder = {
      "RequestId"
  })
  @XmlRootElement(name = "HeaderResponse")
  public class Header {
      @XmlElement(name = "RequestId",required = true)
      protected String RequestId;
  
      public String getRequestId() {
          return RequestId;
      }
  
      public void setRequestId(String RequestId) {
          this.RequestId = RequestId;
      }
  }
  </code></pre>
  
  <p>Calling code</p>
  
  <pre><code>JAXBContext jaxbContext = JAXBContext.newInstance(Header.class);
              SOAPMessage requestMessage = MessageFactory.newInstance().createMessage(null,
                      new ByteArrayInputStream(file.getBytes()));;
              SOAPHeader reqheader = requestMessage.getSOAPHeader();
              Iterator&lt;SOAPHeaderElement&gt; itr = reqheader.examineAllHeaderElements();
                      while (itr.hasNext()) {
                          SOAPHeaderElement ele = itr.next();
  
                          Header headerType=(Header)jaxbContext.createUnmarshaller().unmarshal(ele);
                           System.out.println(headerType.getRequestId());
                      }
  </code></pre>
  
  <p>The exception which I'm getting:</p>
  
  <pre><code>javax.xml.bind.UnmarshalException: unexpected element (uri:"http://example.com", local:"HeaderResponse"). Expected elements are &lt;{}HeaderResponse&gt;
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(Unknown Source)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Unknown Source)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(Unknown Source)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(Unknown Source)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(Unknown Source)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(Unknown Source)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(Unknown Source)
  at com.sun.xml.internal.bind.unmarshaller.DOMScanner.visit(Unknown Source)
  at com.sun.xml.internal.bind.unmarshaller.DOMScanner.scan(Unknown Source)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
  at com.example.model.XYZ.parseAndPrintXMLTagValues(XYZ.java:69)
  at com.example.main.RunJaxB.main(RunJaxB.java:18)
  </code></pre>