File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes DOM Parsing - Please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "DOM Parsing - Please help" Watch "DOM Parsing - Please help" New topic
Author

DOM Parsing - Please help

Vijay Verma
Greenhorn

Joined: Oct 21, 2005
Posts: 5
Hi Guys,

I am trying to run the following very simple program on my machine to parse a very simple XML file.
It just returns Document object NULL.

Same code is working fine on another machine.

Note: there is no silly mistake. i have valid xml file at valid place.

Please help.



import org.apache.xerces.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import java.io.*;

class XML
{
public static void main(String[] args)
{
try{
String caseFile = "c:\\case-config\\config.xml";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(caseFile);
System.out.println("\n\n----" + doc);
}catch(Exception e)
{
e.printStackTrace();
}
}
}
Ulf Dittmer
Rancher

Joined: Mar 22, 2005
Posts: 42956
    
  73
Can you read the file using java.io.File?
Vijay Verma
Greenhorn

Joined: Oct 21, 2005
Posts: 5
Yes I can read files using java.io
Ulf Dittmer
Rancher

Joined: Mar 22, 2005
Posts: 42956
    
  73
What I meant was: can you read this particular file with code in this particular Java class?
Vijay Verma
Greenhorn

Joined: Oct 21, 2005
Posts: 5
yes same xml file i can read with java program..
and i tried with many xml files but no use..

actually when i say factory.setValidating(true);

it gives some errors like Grammer not found etc.
[ October 21, 2005: Message edited by: Vijay Verma ]
Ulf Dittmer
Rancher

Joined: Mar 22, 2005
Posts: 42956
    
  73
Does it work with validation turned off?
Vijay Verma
Greenhorn

Joined: Oct 21, 2005
Posts: 5
no becuase Document object is null.
Makarand Parab
Ranch Hand

Joined: Dec 10, 2004
Posts: 121
Hey Vijay

Try this out and let me know

import org.apache.xerces.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import java.io.*;

class XML
{
public static void main(String[] args)
{
try{
String caseFile = "c:\\case-config\\config.xml";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File(caseFile));
System.out.println("\n\n----" + doc);
}catch(Exception e)
{
e.printStackTrace();
}
}
}
Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19693
    
  10

The code you posted does not tell you that the "doc" variable contains null. It is possible that its toString() method returns null, or the 4-character string "null", as well. And considering that the getNodeValue() method of an Element node does return null (check the API documentation for org.w3c.dom.Node), it's possible you have misidentified the problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DOM Parsing - Please help
 
It's not a secret anymore!