Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to match index values in XPath Java
    primarykey
    data
    text
    <p>Say I've got the following database (generally they will be much bigger):</p> <pre><code>&lt;inventory&gt; &lt;book year="2000"&gt; &lt;title&gt;Snow Crash&lt;/title&gt; &lt;author&gt;Neal Stephenson&lt;/author&gt; &lt;publisher&gt;Spectra&lt;/publisher&gt; &lt;isbn&gt;0553380958&lt;/isbn&gt; &lt;price&gt;14.95&lt;/price&gt; &lt;/book&gt; &lt;book year="2005"&gt; &lt;title&gt;Burning Tower&lt;/title&gt; &lt;author&gt;&lt;/author&gt; &lt;publisher&gt;Pocket&lt;/publisher&gt; &lt;isbn&gt;0743416910&lt;/isbn&gt; &lt;price&gt;5.99&lt;/price&gt; &lt;/book&gt; &lt;book year="1995"&gt; &lt;title&gt;Zodiac&lt;/title&gt; &lt;author&gt;Neal Stephenson&lt;/author&gt; &lt;publisher&gt;Spectra&lt;/publisher&gt; &lt;isbn&gt;0553573862&lt;/isbn&gt; &lt;price&gt;7.50&lt;/price&gt; &lt;/book&gt; &lt;!-- more books... --&gt; </code></pre> <p></p> <p>I want to grab all of the book titles and authors, and then print out the titles and the corresponding authors. Notice that the second entry does not list an author. I've found that XPath will create a list of 3 books, but of only 2 authors. Therefore I can't print the corresponding Book/Author combos by making a loop using the same index values from each list (it will only work for the first entry, then gets off by one, and eventually gives a null pointer exception when there are no more entries in the author list). Is there a way to get around this issue? </p> <p>Thanks for reading.</p> <p>Note: A piece of the code so far: </p> <pre><code>XPathExpression expr1 = xpath.compile("//pre:entry/pre:title/text()"); XPathExpression expr2 = xpath.compile("//pre:entry/pre:author/text()"); Object result1 = expr1.evaluate(doc, XPathConstants.NODESET); Object result2 = expr2.evaluate(doc, XPathConstants.NODESET); NodeList nodes1 = (NodeList) result1; NodeList nodes2 = (NodeList) result2; for (int i = 0; i &lt; nodes1.getLength(); i++) { System.out.println(nodes1.item(i).getNodeValue()); System.out.println(nodes2.item(i).getNodeValue()); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload