| | |
XML file not found for parser?
Thread Solved |
•
•
Join Date: Oct 2007
Posts: 25
Reputation:
Solved Threads: 1
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...
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 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...
Java Syntax (Toggle Plain Text)
//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
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 53
I try this part of your program with my xml-file
further code related to
you not posted, also i don't know what is inside "Bank.xml"
I can't answer, insufficient info.
//and my .... continuation
Element element = doc.getDocumentElement();
System.out.println(element.getTagName());
// is OK XPathFactory xpfactory = XPathFactory.newInstance();
XPath path = xpfactory.newXPath();
//....I can't answer, insufficient info.
•
•
Join Date: Oct 2007
Posts: 25
Reputation:
Solved Threads: 1
•
•
•
•
I try this part of your program with my xml-file
further code related to//and my .... continuation Element element = doc.getDocumentElement(); System.out.println(element.getTagName()); // is OK
you not posted, also i don't know what is inside "Bank.xml"XPathFactory xpfactory = XPathFactory.newInstance(); XPath path = xpfactory.newXPath(); //....
I can't answer, insufficient info.
Java Syntax (Toggle Plain Text)
//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.
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 ?"
"How to ask questions the smart way ?"
•
•
Join Date: Oct 2007
Posts: 25
Reputation:
Solved Threads: 1
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
Thanks again and have a great Christmas!
...Tyster
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 53
Posted code is correct.
It is no
/**
* This class specifies an exceptional condition that occured
* during the transformation process.
*/
public class TransformerException extends Exception {
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...
![]() |
Similar Threads
- How to choose a "right" xml parser of c++ (C++)
- BOM Problem with crimson Parser (Java)
- Searching and Comparing strings from an XML Document (Python)
- Hijackthis report, I just don't know (Viruses, Spyware and other Nasties)
- HackTool.Rootkit virus - Help Needed (Viruses, Spyware and other Nasties)
- system crashes,gets slow, and network gets down,Hijacklog file pvded (Viruses, Spyware and other Nasties)
- System Crashes and installed applications take a lot of time to load (Viruses, Spyware and other Nasties)
- How can I delete Hosts: 1159680172 auto.search.msn.com? (Viruses, Spyware and other Nasties)
- help with pcOrion spyware scanner (Viruses, Spyware and other Nasties)
Other Threads in the Java Forum
- Previous Thread: Launching Executables within java
- Next Thread: Arraylist Problem Help Please
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp game gameprogramming givemetehcodez graphics gui health html ide image integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list login loops mac main map method methods mobile netbeans notdisplaying number online printf problem program project properties qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort sql string swing system textfields threads time title tree tutorial-sample update variablebinding windows working xor






