title: Querying XML with Java
body: <p>I'm able to run simple queries on my XML document using Java, but I'm looking to run a more 'advanced' query and have run into a bit of trouble in doing so. </p>
  
  <p>I am able to get the name of only the first actor of this example (in this case Marlon Brando), when I want to get the names of the rest of the actors in the XML file. </p>
  
  <p>I have used so far <code>/film/cast</code> which shows me all of the actor names, but also the role they play within the film; which I do not want. </p>
  
  <p>Please help! 
  Thanks</p>
  
  <p>XMl:</p>
  
  <pre><code>    &lt;!--?xml version="1.0"?--&gt;
  &lt;film&gt;
      &lt;title&gt;"Godfather, The"&lt;/title&gt;
      &lt;year&gt;1972&lt;/year&gt;
      &lt;directors&gt;
          &lt;director&gt;Francis Ford Coppola&lt;/director&gt;
      &lt;/directors&gt;
      &lt;genres&gt;
          &lt;genre&gt;Crime&lt;/genre&gt;
          &lt;genre&gt;Drama&lt;/genre&gt;
      &lt;/genres&gt;
      &lt;plot&gt;Son of a mafia boss takes over when his father is critically wounded in a mob hit.&lt;/plot&gt;
      &lt;cast&gt;
          &lt;performer&gt;
              &lt;actor&gt;Marlon Brando&lt;/actor&gt;
              &lt;role&gt;Don Vito Corleone&lt;/role&gt;
          &lt;/performer&gt;
          &lt;performer&gt;
              &lt;actor&gt;Al Pacino&lt;/actor&gt;
              &lt;role&gt;Michael Corleone&lt;/role&gt;
          &lt;/performer&gt;
          &lt;performer&gt;
              &lt;actor&gt;Diane Keaton&lt;/actor&gt;
              &lt;role&gt;Kay Adams Corleone&lt;/role&gt;
          &lt;/performer&gt;
          &lt;performer&gt;
              &lt;actor&gt;Robert Duvall&lt;/actor&gt;
              &lt;role&gt;Tom Hagen&lt;/role&gt;
          &lt;/performer&gt;
          &lt;performer&gt;
              &lt;actor&gt;James Caan&lt;/actor&gt;
              &lt;role&gt;Sonny Corleone&lt;/role&gt;
          &lt;/performer&gt;
      &lt;/cast&gt;
  &lt;/film&gt;
  </code></pre>
  
  <p>Java class:</p>
  
  <pre><code>pa
  
  ckage Film;
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.parsers.ParserConfigurationException;
  import javax.xml.xpath.XPath;
  import javax.xml.xpath.XPathExpressionException;
  import javax.xml.xpath.XPathFactory;
  
  import org.w3c.dom.Document;
  import org.xml.sax.SAXException;
  
  public class ASS2_FILM{
      public static void main(String[] args) {
  
          try {
              FileInputStream file = new FileInputStream (new File("/Users/benchalmers/Documents/Uni /Year 2/Database Engineering/Assignment 3/Film/FILM.xml"));
  
              DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
  
              DocumentBuilder builder =  builderFactory.newDocumentBuilder();
  
              Document xmlDocument = builder.parse(file);
  
              XPath xPath =  XPathFactory.newInstance().newXPath();
  
  String expression3 = "/film/cast/";
              System.out.println(expression3);
              String actor = xPath.compile(expression3).evaluate(xmlDocument);
              System.out.println(actor);
  </code></pre>
  
  <p>Returned value:</p>
  
  <pre><code>/film/cast
  
  
              Marlon Brando
              Don Vito Corleone
  
  
              Al Pacino
              Michael Corleone
  
  
              Diane Keaton
              Kay Adams Corleone
  
  
              Robert Duvall
              Tom Hagen
  
  
              James Caan
              Sonny Corleone
  </code></pre>