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 non validation parsing

 
Himanshu Rawat
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Clapham
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its compiling perfectly fine but still throws nullpointer exception.

Which object is null?
 
Himanshu Rawat
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dom
 
Paul Clapham
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps your EntityResolver should return an InputSource containing a copy of the actual DTD then.
 
Himanshu Rawat
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could pre-process the text stream to remove the DTD reference completely.

Bill
 
Paul Clapham
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi William Brogden,

Please tell me how?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic