Groovy XmlSlurper Content Prohibited In Prolog

I am trying to parse an XML file and run this error:

org.xml.sax.SAXParseException: Content is not allowed in prolog

I saw other posts in SO, but my XML document looks fine - no extra characters or spaces 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">

Below is the relevant part of the script (Groovy 1.8.9):

def coveragedata = new XmlSlurper(false,false).parseText(coverageFile)

Thanks for the help.

+4
source share
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 coverageFileis a string containing xml from a file as above, with parseTextinstead parse:

parser.parseText( coverageFile )
+6
source

This code works fine:

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

Source: https://habr.com/ru/post/1535051/


All Articles