Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DOM Parsing - Please help

 
Vijay Verma
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you read the file using java.io.File?
 
Vijay Verma
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I can read files using java.io
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I meant was: can you read this particular file with code in this particular Java class?
 
Vijay Verma
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it work with validation turned off?
 
Vijay Verma
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no becuase Document object is null.
 
Makarand Parab
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20763
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic