title: What is the simplest way to extract an XML node for JAXB.unmarshal()?
body: <p>I use the <code>wsdl2java</code> goal of <code>cxf-codegen-plugin</code> to generate Java from a WSDL.  Then, in my tests, I use JAXB.unmarshal() to populate classes from a raw webservice XML result.  </p>
  
  <p>A typical example is <code>GetAllResponseType response = unmarshal("get-all.xml", GetAllResponseType.class)</code>, using the following method:</p>
  
  <pre><code>&lt;T&gt; T unmarshal(String filename, Class&lt;T&gt; clazz) throws Exception {
      InputStream body = getClass().getResourceAsStream(filename);
      return javax.xml.bind.JAXB.unmarshal(body, clazz);
  }
  </code></pre>
  
  <p>The problem is this: The raw XML response always have enclosing Envelope and Body tags which are not generated as classes by wsdl2java:</p>
  
  <pre><code>&lt;n4:Envelope xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:n="http://www.informatica.com/wsdl/"
           xmlns:n4="http://schemas.xmlsoap.org/soap/envelope/" xmlns:n5="http://schemas.xmlsoap.org/wsdl/"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
    &lt;n4:Body&gt;
      &lt;n:getAllResponse xmlns:n="http://www.informatica.com/wsdl/"&gt;
          &lt;n:getAllResponseElement&gt;
             ...
          &lt;/n:getAllResponseElement&gt;
      &lt;/n:getAllResponse&gt;
    &lt;/n4:Body&gt;
  &lt;/n4:Envelope&gt;
  </code></pre>
  
  <p>So, in order to use JAXB.unmarshal() I have to </p>
  
  <ol>
  <li>either strip away the surrounding Envelope/Body tags manually in get-all.xml</li>
  <li>or extract the getAllResponse node and re-convert it to an InputStream</li>
  <li>or create the Envelope and Body classes</li>
  </ol>
  
  <p>Currently I do <strong>2</strong>, but it's a lot of code:</p>
  
  <pre><code>&lt;T&gt; T unmarshal(String filename, Class&lt;T&gt; clazz) throws Exception {
      InputStream is = getClass().getResourceAsStream(filename);
      InputStream body = nodeContent(is, "n4:Body");
      return javax.xml.bind.JAXB.unmarshal(body, clazz);
  }
  
  InputStream nodeContent(InputStream is, String name) throws Exception {
      DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
      Document doc = docBuilder.parse(is);
      Node node = firstNonTextNode(doc.getElementsByTagName(name).item(0).getChildNodes());
      return nodeToStream(node);
  }
  
  Node firstNonTextNode(NodeList nl) {
      for (int i = 0; i &lt; nl.getLength(); i++) {
          if (!(nl.item(i) instanceof Text)) {
              return nl.item(i);
          }
      }
      throw new RuntimeException("Couldn't find nontext node");
  }
  
  InputStream nodeToStream(Node node) throws Exception {
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      Source xmlSource = new DOMSource(node);
      Result outputTarget = new StreamResult(outputStream);
      TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
      return new ByteArrayInputStream(outputStream.toByteArray());
  }
  </code></pre>
  
  <p>My questions are:</p>
  
  <ul>
  <li>Is there an easier way to the extraction in <strong>2</strong>?  I am tempted to just do a regexp.  I tried XPath, but somehow I couldn't get it to work.  Code examples would be helpful.</li>
  <li>Can I get wsdl2java to create the Body / Envelope classes (<strong>3</strong>), or is it easy to create them myself? </li>
  </ul>