Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7743095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:26:37+00:00 2026-06-01T09:26:37+00:00

Say I’ve got the following database (generally they will be much bigger): <inventory> <book

  • 0

Say I’ve got the following database (generally they will be much bigger):

<inventory>
<book year="2000">
    <title>Snow Crash</title>
    <author>Neal Stephenson</author>
    <publisher>Spectra</publisher>
    <isbn>0553380958</isbn>
    <price>14.95</price>
</book>

<book year="2005">
    <title>Burning Tower</title>
    <author></author>
    <publisher>Pocket</publisher>
    <isbn>0743416910</isbn>
    <price>5.99</price>
</book>

<book year="1995">
    <title>Zodiac</title>
    <author>Neal Stephenson</author>
    <publisher>Spectra</publisher>
    <isbn>0553573862</isbn>
    <price>7.50</price>
</book>

<!-- more books... -->

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?

Thanks for reading.

Note: A piece of the code so far:

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 < nodes1.getLength(); i++) {
   System.out.println(nodes1.item(i).getNodeValue());
   System.out.println(nodes2.item(i).getNodeValue());
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:26:38+00:00Added an answer on June 1, 2026 at 9:26 am

    You have to iterate throw all book elements, then in each book element get title and author elements, e.g. in pseudo-code:

    var bookNodes = XPath.select('//pre:book');
    foreach book in booknodes
        var title = book.select('pre:title');
        var author = book.select('pre:author');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

say i have a nvarchar field in my database that looks like this 1,
Say I have the following: namespace SomeProject { public interface ISomething { string SomeMethod();
Say I have the following template function: template <class T> void apply(const vector<complex<T> >&
Say I have the following code: private Integer number; private final Object numberLock =
Say we have the following method: private MyObject foo = new MyObject(); // and
Say I have the following file structure: app/ app.py controllers/ __init__.py project.py plugin.py If
Say I've got this array: MyArray(0)=aaa MyArray(1)=bbb MyArray(2)=aaa Is there a .net function which
Say using following function: getUnique(){ MyObject obj = getValueFromDb(); obj.modifyIt(); obj.commit(); } When simultaneous
Say we have the following simple data-frame of date-value pairs, where some dates are
Say I have the following: class Base { public Base (int n) { }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.