Hi Folks,

I am encountering what I think is a simple problem but I haven't been able to solve it. I am trying to parse a simple XML file and I keep getting the following exception...

Exception in thread "main" javax.xml.transform.TransformerException: A location step was expected following the '/' or '//' token.
at com.sun.org.apache.xpath.internal.compiler.XPathParser.error(Unknown Source)

... more lines ...

Caused by: javax.xml.transform.TransformerException: A location step was expected following the '/' or '//' token.
at com.sun.org.apache.xpath.internal.compiler.XPathParser.error(Unknown Source)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.RelativeLocationPath(Unknown Source)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.LocationPath(Unknown Source)

I am convinced the problem is that the file isn't being found so the parser returns the exception above. I have built my documentBuilder and XPath objects correctly. Here is the code that I am using...

//Create builder and xpath objects
		DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder builder = dbfactory.newDocumentBuilder();
	    XPathFactory xpfactory = XPathFactory.newInstance();
	    XPath path = xpfactory.newXPath();

 //Find the XML file and start parsing 
 String fName = "C:\\Bank.xml";      // Windows path
 File f = new File(fName);
 Document doc = builder.parse(f);   // Parsing

I've tried various adjustments to get the parser to find the file, including simply specifyng the filename by itself (no specific path). I am using Eclipse and included the file in the project so I thought it would be found there without a problem but I guess not. In any case, nothing I've tried has worked. Can anyone kindly offer any suggestions?

Any help is greatly appreciated!

Thanks and have a great weekend!

Tyster

I try this part of your program with my xml-file

//and my .... continuation
        Element element = doc.getDocumentElement();
        System.out.println(element.getTagName());
// is OK

further code related to

XPathFactory xpfactory = XPathFactory.newInstance();
        XPath path = xpfactory.newXPath();
//....

you not posted, also i don't know what is inside "Bank.xml"
I can't answer, insufficient info.

I try this part of your program with my xml-file

//and my .... continuation
        Element element = doc.getDocumentElement();
        System.out.println(element.getTagName());
// is OK

further code related to

XPathFactory xpfactory = XPathFactory.newInstance();
        XPath path = xpfactory.newXPath();
//....

you not posted, also i don't know what is inside "Bank.xml"
I can't answer, insufficient info.

Thanks for your help. I think my document and xpath code is OK. I think the problem is that my XML file is not (cannot) be found by the parser. I suspect the real problem is in this part of my code...

//Find the XML file and start parsing 
 String fName = "C:\\Bank.xml";      // Windows path
 File f = new File(fName);
 Document doc = builder.parse(f);   // Parsing

I'm sure I'm making a silly mistake. The file is located in C:\Bank.xml Is my syntax wrong? I'm not sure I understand why the file would not be found (see the exception in my forst post).

Any advice is greatly appreciated!

...Tyster

My advice would be to confirm that really the file cannot be found. Take a look at the javadocs of File class. It has an "exists()" method using which you can really confirm whether the file can be read by your Java program or not.
If the "exists()" method returns false thens its confirmed that your code cannot reach the XML file but if ii returns true then I guess you have to check your XML file.
Now in case it returns false, you could try moving the xml file inside the folder which holds the your Eclipse Project (not the workspace, project folder is inside the workspace) and use just the relative path (i.e "blank.xml" if it is placed directly inside the project folder).

Also keep in mind the case of the filename in Java. Although I cannot confirm it since I am mostly on Linux, *I think* Java is case sensitive about file / folder names even though Windows is not, so also try renaming your file to all lower case and do the same while accessing it from your program to see if it works.

Hope it helps.

commented: Many Thanks for your help! +2

stephen84... Thanks for the suggestion. It turns out the code I have was correct... I used the exist() method as you suggested and it returned true. Turns out that the real source of the exception I was getting was my incorrect use of XPath syntax (right after the code I pasted above). I've corrected that and all is well.

Thanks again and have a great Christmas!

...Tyster

Posted code is correct.
It is no java.io.IOException. It is javax.xml.transform.TransformerException ==> A location step was expected following the '/' or '//' token. /**
* This class specifies an exceptional condition that occured
* during the transformation process.
*/
public class TransformerException extends Exception {

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.