Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy XmlSlurper Content is not allowed in prolog

Tags:

xml

groovy

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.

like image 354
JamesE Avatar asked Apr 04 '14 14:04

JamesE


People also ask

What is XML prolog?

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.


2 Answers

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 )
like image 60
tim_yates Avatar answered Oct 10 '22 03:10

tim_yates


This code works fine:

def coveragedata = new XmlSlurper(false,false,true).parseText(coverageFile)
println coveragedata.'@branch-rate'
like image 42
Opal Avatar answered Oct 10 '22 05:10

Opal