title: XPath expression to return an array of node names and their text content
body: <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;shop&gt;
      &lt;order&gt;
          &lt;name&gt;Mr A Beasy&lt;/name&gt;
          &lt;date&gt;12/03/2017&lt;/date&gt;
          &lt;payment&gt;Debit Card&lt;/payment&gt;
      &lt;/order&gt;
      &lt;order&gt;
          &lt;name&gt;Mrs D Ehffi&lt;/name&gt;
          &lt;date&gt;13/04/2017&lt;/date&gt;
          &lt;payment&gt;Paypal&lt;/payment&gt;
      &lt;/order&gt;
  &lt;/shop&gt;
  </code></pre>
  
  <p>Using the above xml as an example, I am trying to retrieve each 'order' along with the node names and text contents contained in it. I want to eventually save each <code>name</code>, <code>date</code> and <code>payment</code>'s text content to a corresponding field in a java object.</p>
  
  <p>The problem is, the <code>name</code>, <code>date</code> and <code>payment</code> nodes are not always guaranteed to be there. So I need a way to first check if the <code>name</code> node exists and is not null, then extract its text content and save that to my java object.</p>
  
  <p>I have tried:</p>
  
  <pre><code>NodeList nodeList = (NodeList) path.evaluate("/shop/order", doc, XPathConstants.NODESET);
  String name = nodeList.item(i).getNodeName();
  String text = nodeList.item(i).getTextContent();
  </code></pre>
  
  <p>I hoped this would give me a nice array with two 'order' nodesets that I could iterate through and get the values of the nodes and their contents, but it doesn't work. For some reason my <code>nodeList</code> has a bunch of <code>\n</code> in it.</p>
  
  <p>I am unsure of how to search my <code>nodeList</code> for a specific node called name, and if it exists extract its value. Can anyone please help?</p>