title: Retrieve value of XML node and node attribute using XPath in JAXP
body: <p>Given an xml document that looks like the following:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"&gt;
  &lt;properties&gt;
      &lt;entry key="agentType"&gt;STANDARD&lt;/entry&gt;
      &lt;entry key="DestinationTransferStates"&gt;&lt;/entry&gt;
      &lt;entry key="AgentStatusPublishRate"&gt;300&lt;/entry&gt;
      &lt;entry key="agentVersion"&gt;f000-703-GM2-20101109-1550&lt;/entry&gt;
      &lt;entry key="CommandTimeUTC"&gt;2010-12-24T02:25:43Z&lt;/entry&gt;
      &lt;entry key="PublishTimeUTC"&gt;2010-12-24T02:26:09Z&lt;/entry&gt;
      &lt;entry key="queueManager"&gt;AGENTQMGR&lt;/entry&gt;
  &lt;/properties&gt;
  </code></pre>
  
  <p>I want to print the values of the "key" attribute and the element so it looks like this:</p>
  
  <pre><code>agentType = STANDARD
  DestinationTransferStates = 
  AgentStatusPublishRate = 300
  agentVersion = f000-703-GM2-20101109-1550
  CommandTimeUTC = 2010-12-24T02:25:43Z
  PublishTimeUTC = 2010-12-24T02:26:09Z
  queueManager = AGENTQMGR
  </code></pre>
  
  <p>I'm able to print the node values with no problem using this code:</p>
  
  <pre><code>XPathFactory factory = XPathFactory.newInstance();
  XPath xpath = factory.newXPath();
  XPathExpression expr = xpath.compile("//properties/entry/text()");
  
  Object result = expr.evaluate(doc, XPathConstants.NODESET);
  NodeList nodes = (NodeList) result;
  for (int i = 0; i &lt; nodes.getLength(); i++) {
      System.out.println(nodes.item(i).getNodeValue()); 
  }
  </code></pre>
  
  <p>And I can print the values of the "key" attribute by changing the xpath expression and the node methods as follows:</p>
  
  <pre><code>XPathFactory factory = XPathFactory.newInstance();
  XPath xpath = factory.newXPath();
  XPathExpression expr = xpath.compile("//properties/entry");
  Object result = expr.evaluate(doc, XPathConstants.NODESET);
  NodeList nodes = (NodeList) result;
  for (int i = 0; i &lt; nodes.getLength(); i++) {
      System.out.println(nodes.item(i).getAttributes().getNamedItem("key").getNodeValue()); 
  }
  </code></pre>
  
  <p>It seems like there would be a way to get at both of these values in a single <code>evaluate</code>.  I could always evaluate two NodeLists and iterate through them with a common index but I'm not sure they are guaranteed to be returned in the same order.  Any suggestions appreciated.</p>