• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

DOM non validation parsing

 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 28395
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dom
 
Paul Clapham
Sheriff
Posts: 28395
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 28395
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could pre-process the text stream to remove the DTD reference completely.

Bill
 
Paul Clapham
Sheriff
Posts: 28395
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi William Brogden,

Please tell me how?
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 28395
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic