![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jul 2005
Location: Athens,Greece
Posts: 39
Rep Power: 0
![]() |
JAVA XML Parsing
Hello there, i am trying parsing a xml file into a document using java, but i am getting an exception. Below is the code:
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
public class testDOM
{
public void main (String[] args)
{
try
{
String File = "settings.xml";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.parse(File);
System.out.println("\n\n----" + builder.parse(File));
}catch(Exception e)
{
e.printStackTrace();
}
}
}And the exception is: C:\j2sdk1.4.2_10\bin\java -Didea.launcher.port=7538 "-Didea.launcher.bin.path=C:\Program Files\JetBrains\IntelliJ IDEA 5.0\bin" -Dfile.encoding=windows-1253 -classpath "C:\j2sdk1.4.2_10\jre\lib\charsets.jar;C:\j2sdk1.4.2_10\jre\lib\jce.jar;C:\j2sdk1.4.2_10\jre\lib\jsse.jar;C:\j2sdk1.4.2_10\jre\lib\plugin.jar;C:\j2sdk1.4.2_10\jre\lib\rt.jar;C:\j2sdk1.4.2_10\jre\lib\sunrsasign.jar;C:\j2sdk1.4.2_10\jre\lib\ext\dnsns.jar;C:\j2sdk1.4.2_10\jre\lib\ext\ldapsec.jar;C:\j2sdk1.4.2_10\jre\lib\ext\localedata.jar;C:\j2sdk1.4.2_10\jre\lib\ext\sunjce_provider.jar;C:\j2sdk1.4.2_10;C:\Projects\testDOM\classes;C:\Dom4J\dom4j-1.6.1.jar;C:\Program Files\JetBrains\IntelliJ IDEA 5.0\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain testDOM java.lang.NullPointerException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:324) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:86) Exception in thread "main" Process finished with exit code 1 Any ideas? I tried to compile it using dom4j library but i have a problem with the Document declaration. Any help will be appreciated Thx in advance |
|
|
|
|
|
#2 |
|
Expert Programmer
|
First of all, you should make the variable File start with a lowercase letter, or it appears to be a class name. This will confuse many Java developers and is considered bad form.
The problem might be that you are passing in a String rather than File variable for the XML file's location. You may also need to use an absolute path to the file you are trying to use. So try the following: import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
public class testDOM {
public void main(String[] args) {
try {
File file = "/path/to/settings.xml";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(file);
System.out.println("\n\n----" + dom);
} catch(Exception e) {
e.printStackTrace();
}
}
} |
|
|
|
|
|
#3 |
|
Expert Programmer
|
I made a mistake in the code above: File file = "/path/to/settings.xml"; should be File file = new File("/path/to/settings.xml");.
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Jul 2005
Location: Athens,Greece
Posts: 39
Rep Power: 0
![]() |
Thx my friend, finally i tried to do it using SAXBuilder and it worked ok, maybe it was a problem because i was using dom4j library and i had some problems
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|