Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: java.lang.String cannot be cast to org.w3c.dom.Node

Tags:

java

xml

xpath

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?

like image 393
Asdfg Avatar asked Oct 08 '22 09:10

Asdfg


1 Answers

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();
        }
    }
}
like image 164
Jasonw Avatar answered Oct 13 '22 10:10

Jasonw