Question

I have the following code snippet in a Gradle script (the syntax is a combination of Groovy/Java):

File file = new File(filename) // Filename is being read from console
def content = file.getText()
Document document = DOMBuilder.parse(new StringReader(content), false, false)

The problem is, I'm trying to parse an XML file, but with a xconf extension (e.g. file.xconf). For some reason or another, when I try the code above, I get the following error message (in the console):

java.io.FileNotFoundException: <full_path>/file.dtd (No such file or directory)

The path is correct, but I noticed the extension is suddenly being changed to .dtd. I noticed in the file there's a reference to the .dtd version of that file, but I want the parser to ignore that (and stop validation, which is why the 2nd argument of DOMBuilder.parse() is false). Can I change this behavior to be able to succesfully parse the file?

Note: If possible, I would also like to be able to do the same with (any) other file extension.

Thanks in advance!

Was it helpful?

Solution

Try this:

import groovy.xml.*
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

Document parseWithoutDTD( Reader r, boolean validating=false, boolean namespaceAware=true ) {
  FactorySupport.createDocumentBuilderFactory().with { f ->
    f.namespaceAware = namespaceAware
    f.validating = validating
    f.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    f.newDocumentBuilder().with { db ->
      db.parse( new InputSource( r ) )
    }
  }
}

Document d = new File( filename ).withReader { r ->
  parseWithoutDTD( r )
}

OTHER TIPS

The file.xconf xml must have defined DTD reference in the XML. Open the file and check.

The DOM parser by default validates XML against the DTD associated with the XML. If you not want to validate set the validation mode to false. Check the DomBuilder API.

Other option is to define the DTD path in XML so that its accessible from anywhere or you can register a resolver also. I forgot how to do that but you can lookup.

A DTD defines the rules for XML documents. If you follow these rules, the xml document is considered as "valid". If you want to ignore the DTD file (e.g. because it doesn't exist), set the validation mode of your parse to false.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top