XPath + Erro

Estou executnado esse código :

public class PesquisaXml
{
    /**
     *  
     */
    public PesquisaXml()
    {
        
        XPath xpath = XPathFactory.newInstance().newXPath();
        String expression = "/catalog/journal/article";
        InputSource inputSource = new InputSource("catalago.xml");
      
        try{
            NodeSet nodes = (NodeSet) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
        }
        catch(XPathExpressionException e){
            e.printStackTrace();
        }

    }
    public static void main(String[] args)
    {	
        PesquisaXml obj = new PesquisaXml();
    }
}

E está dando esse erro :

java.lang.ClassCastException: com.sun.org.apache.xml.internal.dtm.ref.DTMNodeList

Alguem sabe q erro eh esse ??

valeu !!

Po cara não consegui rodar de jeito nenhum essa merda do exemplo do XPath, comigo deu um erro mais louco:

java.io.IOException: No such file or directory
        at java.io.FileInputStream.read(Native Method)
        at com.sun.org.apache.xerces.internal.impl.XMLEntityManager$RewindableInputStream.read(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
        at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(Unknown Source)
        at XPathEvaluator.evaluateDocument(XPathEvaluator.java:28)
        at XPathEvaluator.main(XPathEvaluator.java:45)
--------------- linked to ------------------
javax.xml.xpath.XPathExpressionException
        at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(Unknown Source)
        at XPathEvaluator.evaluateDocument(XPathEvaluator.java:28)
        at XPathEvaluator.main(XPathEvaluator.java:45)Caused by: java.io.IOException: No such file or directory
        at java.io.FileInputStream.read(Native Method)
        at com.sun.org.apache.xerces.internal.impl.XMLEntityManager$RewindableInputStream.read(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
        ... 3 more

Código:

import javax.xml.xpath.*;
import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import org.xml.sax.InputSource;
import com.sun.org.apache.xpath.internal.NodeSet;
import javax.xml.namespace.NamespaceContext;


public class XPathEvaluator{


 public void evaluateDocument(File xmlDocument){

  try{
    XPathFactory factory=XPathFactory.newInstance();
    XPath xPath=factory.newXPath();
    InputSource inputSource=new InputSource(new FileInputStream(xmlDocument));
    XPathExpression xPathExpression=xPath.compile("/catalog/journal/article[@date='January-2004']/title");
    String title=xPathExpression.evaluate(inputSource);
    System.out.println("Title: "+ title);

    inputSource=new InputSource(new FileInputStream(xmlDocument));
    String publisher=xPath.evaluate("/catalog/journal/@publisher", inputSource);
    System.out.println("Publisher:"+ publisher);    

    String expression="/catalog/journal/article";
    NodeSet nodes = (NodeSet) xPath.evaluate(expression, inputSource,     XPathConstants.NODESET);    
	NodeList nodeList = (NodeList) nodes;    
  
  } catch(IOException  e) {
    e.printStackTrace();
  } catch(XPathExpressionException e2) {
    System.out.println("merda!!!!!!!!!!!!!!!!!!!\n\n");
    e2.printStackTrace();
  }
 }

 public static void main(String[] argv){


   XPathEvaluator evaluator=new XPathEvaluator();

   File xmlDocument=new File("c:\catalog.xml");
   evaluator.evaluateDocument(xmlDocument);

 }

}

Dá erro aqui:

NodeSet nodes = (NodeSet) xPath.evaluate(expression, inputSource,     XPathConstants.NODESET); 

Colega o teu erro parece umito tosco também, coloca o Stack Trace dele ae…

Eu troquei a linha q tá dando erro :

NodeSet nodes = (NodeSet) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);

por essa :

NodeList nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);

E deu certo !!! :smiley:

Agora eu acho q sei qual eh seu erro : Se tu já ta utilizando um InputSource para q tu vai usar um FileInputStream ? Faça apenas isso :

 InputSource inputSource = new InputSource("catalago.xml");