我试图解析XML文件并遇到此错误:
org.xml.sax.SAXParseException: Content is not allowed in prolog
Run Code Online (Sandbox Code Playgroud)
我在SO上看过其他帖子,但我的XML文档看起来还不错 - 在XML声明之前没有额外的字符或空格.
<?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">
Run Code Online (Sandbox Code Playgroud)
这是脚本的相关部分(Groovy 1.8.9):
def coveragedata = new XmlSlurper(false,false).parseText(coverageFile)
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助.
你应该能够做到这一点:
def parser = new XmlSlurper()
parser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true)
def coverageData = parser.parse( coverageFile )
Run Code Online (Sandbox Code Playgroud)
或者如果coverageFile是包含文件中xml的String,如上所述,parseText而不是parse:
parser.parseText( coverageFile )
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6087 次 |
| 最近记录: |