Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 27th, 2005, 8:11 AM   #1
tsofras
Programmer
 
tsofras's Avatar
 
Join Date: Jul 2005
Location: Athens,Greece
Posts: 39
Rep Power: 0 tsofras is an unknown quantity at this point
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
tsofras is offline   Reply With Quote
Old Dec 27th, 2005, 12:58 PM   #2
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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();
        }
    }
}
titaniumdecoy is offline   Reply With Quote
Old Dec 27th, 2005, 3:52 PM   #3
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
I made a mistake in the code above: File file = "/path/to/settings.xml"; should be File file = new File("/path/to/settings.xml");.
titaniumdecoy is offline   Reply With Quote
Old Dec 28th, 2005, 1:32 AM   #4
tsofras
Programmer
 
tsofras's Avatar
 
Join Date: Jul 2005
Location: Athens,Greece
Posts: 39
Rep Power: 0 tsofras is an unknown quantity at this point
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
tsofras is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:16 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC