title: Parsing Xml leaf node element values using JAXB
body: <p>I have a xsd say <code>request.xsd</code> and corresponding jaxb generated classes. Now I got a xml file <code>request.xml</code> which I am able to unmarshal and create "request" object. </p>
  
  <p>I have many element tags in xml some of which are available multiple times.  I need to create an java.util.List which should have all leaf node values. </p>
  
  <p>For example : </p>
  
  <p>Below is my request.xml :</p>
  
  <pre><code>&lt;Request&gt;
    &lt;Operation&gt;manual&lt;/Operation&gt;
    &lt;Work&gt;
       &lt;WorkModule&gt;
            &lt;Name&gt;AXN&lt;/Name&gt;
       &lt;/WorkModule&gt;
    &lt;/Work&gt;
    &lt;Identifier&gt;
       &lt;WorkStatus&gt;
            &lt;WorkName&gt;CCH&lt;/WorkName&gt;
       &lt;/WorkStatus&gt;
       &lt;WorkStatus&gt;
            &lt;WorkName&gt;TMH&lt;/WorkName&gt;
       &lt;/WorkStatus&gt;
    &lt;/Identifier&gt;
  &lt;/Request&gt;
  </code></pre>
  
  <p>Below is my JAXB generated Request class. Simillarly there are other classes corresponding to each xml element:</p>
  
  <pre><code>@XmlAccessorType(XmlAccessType.FIELD)
  @XmlType(name = "", propOrder = {
      "Operation",
      "Work",
      "Identifier"
  })
  @XmlRootElement(name = "Request", namespace = "http://www.sprts.com/clm/nso/mahsgd")
  public class Request{
  
      @XmlElement(name = "Operation", required = true)
      protected Operation operation;
      @XmlElement(name = "Work", required = true)
      protected Work work;
      @XmlElement(name = "Identifier", required = true)
      protected Identifier identifier;
  
      \\ getters and setters
  }
  </code></pre>
  
  <p>So using JAXB I can get unmarshalled Request object having all values in xml file.</p>
  
  <p>Now how do I get all leaf node values (operation, name, workName) in a generic manner without using getters from request object and each of which then I can put in a some collection let say List. I have heard of DOM being used to do similar stuff but I need to use JAXB for the same.</p>
  
  <p>(Without using getters from request object like 
    <code>String opertaion = request.getOperation();</code> or <code>String name = request.getWork().getWorkModule().getName();</code>)</p>
  
  <p>--EDIT--</p>
  
  <p>Can someone help me out in finding an optimal solution to this. Let me know if problem statement is not clear.</p>
  
  <p>--EDIT--
  With Doughan &amp; Alexandros's help and some around could able to achieve the same. Not sure if the work around (converting JAXB object to DOM object to InputSource) is the best solution. Below is the working code.</p>
  
  <pre><code>     JAXBContext jc = JAXBContext.newInstance(JAXBObject.class);
  
       // Create the Document
       DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
       DocumentBuilder db = dbf.newDocumentBuilder();
       Document document = db.newDocument();
  
       // Marshal the Object to a Document
       Marshaller marshaller = jc.createMarshaller();
       marshaller.marshal(jaxbObject, document);
  
      XPathFactory xpf = XPathFactory.newInstance();
      XPath xp = xpf.newXPath();
  
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      Source xmlSource = new DOMSource(document);
      Result outputTarget = new StreamResult(outputStream);
      TransformerFactory.newInstance().newTransformer().transform(xmlSource,outputTarget);
      InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
      InputSource source = new InputSource(is);
  
      NodeList leafNodeObjects = (NodeList) xp.evaluate("//*[not(*)]", source, XPathConstants.NODESET);
  
      for(int x=0; x&lt;leafNodeObjects.getLength(); x++) {
                      System.out.print("nodeElement = ");
                      System.out.print(leafNodeObjects.item(x).getNodeName());
                      System.out.print(" and node value = ");
                      System.out.println(leafNodeObjects.item(x).getTextContent());
                      inputDtos.add(new InputDto(leafNodeObjects.item(x).getNodeName(),
                              leafNodeObjects.item(x).getTextContent()));
     }
  </code></pre>