JamesE
JamesE

Reputation: 3923

Groovy XmlSlurper Content is not allowed in prolog

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.

Upvotes: 5

Views: 8682

Answers (2)

tim_yates
tim_yates

Reputation: 171144

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 )

Upvotes: 8

Opal
Opal

Reputation: 84844

This code works fine:

def coveragedata = new XmlSlurper(false,false,true).parseText(coverageFile)
println coveragedata.'@branch-rate'

Upvotes: 2

Related Questions