I am trying to parse an XML file and running into this error:
org.xml.sax.SAXParseException: Content is not allowed in prolog
I've seen the other posts on SO, but my XML document looks OK - no extra characters or white space before the XML declaration.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-04.dtd">
<coverage branch-rate="0.24074074074074073" branches-covered="39" branches-valid="162" complexity="0" line-rate="0.3485915492957746" lines-covered="198" lines-valid="568" timestamp="1396622452625" version="0.2.6">
Here is the relevant portion of the script (Groovy 1.8.9):
def coveragedata = new XmlSlurper(false,false).parseText(coverageFile)
Thank you for the assistance.
The prolog consists of an XML declaration, possibly followed by a document type declaration. The body is made up of a single root element, possibly with some comments and/or processing instructions. An XML document is typically a computer file whose contents meet the requirements laid out in the XML specification.
You should be able to do this:
def parser = new XmlSlurper()
parser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true)
def coverageData = parser.parse( coverageFile )
Or if coverageFile
is a String containing the xml from the file, as above, with parseText
instead of parse
:
parser.parseText( coverageFile )
This code works fine:
def coveragedata = new XmlSlurper(false,false,true).parseText(coverageFile)
println coveragedata.'@branch-rate'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With