java - Parse any file type as XML -
i have following code snippet in gradle script (the syntax combination of groovy/java):
file file = new file(filename) // filename being read console def content = file.gettext() document document = dombuilder.parse(new stringreader(content), false, false)
the problem is, i'm trying parse xml file, xconf
extension (e.g. file.xconf
). reason or another, when try code above, following error message (in console):
java.io.filenotfoundexception: <full_path>/file.dtd (no such file or directory)
the path correct, noticed extension being changed .dtd
. noticed in file there's reference .dtd
version of file, want parser ignore (and stop validation, why 2nd argument of dombuilder.parse()
false). can change behavior able succesfully parse file?
note: if possible, able same (any) other file extension.
thanks in advance!
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 ) }