wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes DOM non validation parsing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "DOM non validation parsing" Watch "DOM non validation parsing" New topic
Author

DOM non validation parsing

Himanshu Rawat
Ranch Hand

Joined: Nov 27, 2005
Posts: 141
Hi,

Please tell me how to parse XML file through DOM without conforming it to any DTD.

DocumentBuilderFactory has got function setValidating(boolean validating) which I'm setting to false but it's still looking for dtd in current directory.

It seems this function is not affecting.

I want to bypass this??

Thanks
Rawat


rawat
SCJP 1.4
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

That's because DTDs perform other services besides validation. Replacement of entity declarations, for one, and there are others. So just ignoring the DTD might not be a good idea in general.

However it might be possible for you to use an EntityResolver to return an empty document in place of the DTD.
Himanshu Rawat
Ranch Hand

Joined: Nov 27, 2005
Posts: 141
Hi paul,

I tried using entity resolver but still its giving exception.

Below is the code.

parser.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(java.lang.String publicId,
java.lang.String systemId) throws SAXException,
java.io.IOException {
System.out.println("systemId =" + systemId);

if (systemId
.equals("log4j.dtd")) {
System.out.println("Matched"); -->> its printing this..
return new InputSource(new ByteArrayInputStream("".getBytes()));
} else
return null;
}
});
try {
dom = parser.parse(new File("log.xml"));
} catch (Exception e) {
e.printStackTrace();
}

Its compiling perfectly fine but still throws nullpointer exception. Please correct me???

Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41108
    
  45
Its compiling perfectly fine but still throws nullpointer exception.

Which object is null?


Ping & DNS - my free Android networking tools app
Himanshu Rawat
Ranch Hand

Joined: Nov 27, 2005
Posts: 141
dom
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Then an exception is being thrown. You are printing the stack trace, why not look at the output? (And by that I mean, why not tell us what's in the output?)
Himanshu Rawat
Ranch Hand

Joined: Nov 27, 2005
Posts: 141
Hi paul,

Below is the exception

Matched
java.lang.NullPointerException
at org.apache.crimson.parser.Parser2.parseInternal(Parser2.java:523)
at org.apache.crimson.parser.Parser2.parse(Parser2.java:304)
at org.apache.crimson.parser.XMLReaderImpl.parse(XMLReaderImpl.java:433)
at org.apache.crimson.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:179)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:208)
at ipunity.apps.vm.rep.TransferReports.readLogProperties(TransferReports.java:259)
at ipunity.apps.vm.rep.TransferReports.main(TransferReports.java:67)
Caused by: java.lang.NullPointerException
at org.apache.crimson.tree.XmlDocumentBuilder.startElement(XmlDocumentBuilder.java:476)
at org.apache.crimson.parser.Parser2.maybeElement(Parser2.java:1449)
at org.apache.crimson.parser.Parser2.parseInternal(Parser2.java:499)
... 6 more


please tell me what am doing wrong
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Perhaps your EntityResolver should return an InputSource containing a copy of the actual DTD then.
Himanshu Rawat
Ranch Hand

Joined: Nov 27, 2005
Posts: 141
hi Paul,

But again i will ran into same problem.. it will again try to conform against DTD which i don't want.

Any other way to do it?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
You could pre-process the text stream to remove the DTD reference completely.

Bill
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Himanshu Rawat wrote:But again i will ran into same problem.. it will again try to conform against DTD which i don't want.


I missed where you explained why you don't want that. Why don't you want to apply the DTD?
Himanshu Rawat
Ranch Hand

Joined: Nov 27, 2005
Posts: 141
Hi paul,

I just want to read few values from log.xml file and i don't have any DTD against it. So naturally, I've to avoid any conformance against DTD.

Please help me

Himanshu Rawat
Ranch Hand

Joined: Nov 27, 2005
Posts: 141
Hi William Brogden,

Please tell me how?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
How to pre-process the input: Actually there are several ways

1. for small XML documents - read the whole thing into a String, locate the DTD reference and remove it, create a java.io.InputStream from the String and use that as input to the parser.

2. for larger documents, create a custom implementation of java.io.FilterInputStream which can read the input stream from the file until it finds the DTD reference and skips it. This InputStream now becomes input to the parser. You may have to do a bit of fiddling to make the parser happy with the start of the input stream.

Bill
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Himanshu Rawat wrote:I just want to read few values from log.xml file and i don't have any DTD against it. So naturally, I've to avoid any conformance against DTD.

But your post suggests this is log4j.dtd you are asking about. What do you mean, you don't have it? You have a URL for it in your XML document. If you're running this program in an environment where you can't access it because of a firewall or something, then go somewhere else and download it so you have a local copy. Then use the EntityResolver to resolve to that local copy.
 
wood burning stoves
 
subject: DOM non validation parsing
 
Similar Threads
what is dtd and dft?
Passed it!
Dom and Sax parser
App chokes when SAX-parsing an XML file without a DTD file
Creating XML -- Need help