5

I am trying to parse xml string and I am getting java.lang.String cannot be cast to org.w3c.dom.Node error.

This is the code I am using:

        XPathFactory xPathFactory = XPathFactory.newInstance();

        XPath xPath = xPathFactory.newXPath();

        String expression = "//Home/ListOfCustomers";

        XPathExpression xPathExpression = xPath.compile(expression);

        Object nl = xPathExpression.evaluate(xmlResp);

This is how the XML string is constructed:

<?xml version="1.0" encoding="ISO-8859-1"?>
<Home>
      <ListOfCustomers type="Regular" count="939">
           <Customer>
            <CustName>xyz</CustName>
           </Customer>
           <Customer>
            <CustName>abc</CustName>
           </Customer>
           <Customer>
            <CustName>def</CustName>
           </Customer>
       </ListOfCustomers>
</Home>

What am I missing here?

James Dunn
  • 8,064
  • 13
  • 53
  • 87
Asdfg
  • 11,362
  • 24
  • 98
  • 175
  • @person-who-downvoted-but-didnt-care-to-leave-comment: $%#& &*$ – Asdfg Mar 15 '12 at 01:45
  • Careful, comments like that are likely to get you more downvotes. Don't take a downvote personally, ask yourself if you have followed the guidelines in the [faq] and [ask]. Also recognize that you're going to get some unexplained downvotes... that's just the nature of SO. – Jim Garrison Mar 15 '12 at 02:22
  • Sometime I am confused too, to see downvote on question with programming code. To me, those are all valid question for sof. :-) anyway, my upvote for you. cheers. – Jasonw Mar 15 '12 at 02:37
  • upvoted as well..... – NullPumpkinException Nov 25 '14 at 04:36

2 Answers2

2

Just having a quick look at the docs: http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/xpath/XPathExpression.html#evaluate(java.lang.Object)

Then the api defines this for compile: item - The starting context (node or node list, for example).

So assuming this is the method you are using, it looks like you need to send in a node - or node list, not just a String.

Matt Harrison
  • 338
  • 3
  • 10
2

Object nl = xPathExpression.evaluate(xmlResp);

This is the issue here. With a single argument to the evaluate method, it expect either variable of type InputSource or Object, have you declare xmlResp to either of it? Also, both of these methods return of type String, so why are you assign to a variable of type Object?

Since you have the xml file, why don't you initialize your xmlResp to type of InputSource? Then use the xPathExpression evaluate on the inputsource? Something like the following.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;


public class XMLParser
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        try {

        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xPath = xPathFactory.newXPath();

        InputSource doc = new InputSource(new InputStreamReader(new FileInputStream(new File("file.xml"))));

        String expression = "//Home/ListOfCustomers";
        XPathExpression xPathExpression = xPath.compile(expression);

        NodeList elem1List = (NodeList) xPathExpression.evaluate(doc, XPathConstants.NODESET);
        xPathExpression = xPath.compile("@type");

        for (int i = 0; i < elem1List.getLength(); i++)
        {
            System.out.println(xPathExpression.evaluate(elem1List.item(i), XPathConstants.STRING)); 
        }


        }
        catch (XPathExpressionException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
Jasonw
  • 5,054
  • 7
  • 43
  • 48
  • pretty close. i did not have xml file. it was xml string. i did not convert it to Document first. that was the error. – Asdfg Mar 15 '12 at 05:07
  • 2
    @Asdfg Too bad you didn't give advice if we only have a String (like in messaging service for example), its here: https://stackoverflow.com/questions/16712391/parse-xml-simple-string-using-java-xpath – pdem Aug 03 '18 at 16:21