XML file not found for parser?

Thread Solved

Join Date: Oct 2007
Posts: 25
Reputation: Tyster is an unknown quantity at this point 
Solved Threads: 1
Tyster Tyster is offline Offline
Light Poster

XML file not found for parser?

 
0
  #1
Dec 13th, 2008
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...


  1.  
  2. //Create builder and xpath objects
  3. DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
  4. DocumentBuilder builder = dbfactory.newDocumentBuilder();
  5. XPathFactory xpfactory = XPathFactory.newInstance();
  6. XPath path = xpfactory.newXPath();
  7.  
  8. //Find the XML file and start parsing
  9. String fName = "C:\\Bank.xml"; // Windows path
  10. File f = new File(fName);
  11. 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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz

Re: XML file not found for parser?

 
0
  #2
Dec 13th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 25
Reputation: Tyster is an unknown quantity at this point 
Solved Threads: 1
Tyster Tyster is offline Offline
Light Poster

Re: XML file not found for parser?

 
0
  #3
Dec 13th, 2008
Originally Posted by quuba View Post
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...

  1.  
  2. //Find the XML file and start parsing
  3. String fName = "C:\\Bank.xml"; // Windows path
  4. File f = new File(fName);
  5. 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
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: XML file not found for parser?

 
1
  #4
Dec 14th, 2008
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.
Last edited by stephen84s; Dec 14th, 2008 at 2:05 am.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 25
Reputation: Tyster is an unknown quantity at this point 
Solved Threads: 1
Tyster Tyster is offline Offline
Light Poster

Re: XML file not found for parser?

 
0
  #5
Dec 14th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz

Re: XML file not found for parser?

 
0
  #6
Dec 14th, 2008
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 {
Last edited by quuba; Dec 14th, 2008 at 9:35 pm. Reason: outdated during editing...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC