This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
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.
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
posted
0
Hi paul,
I tried using entity resolver but still its giving exception.
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
posted
0
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
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
posted
0
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
posted
0
Hi William Brogden,
Please tell me how?
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12327
1
posted
0
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.
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.