最近的項目,須要用groovy腳本解析info.plist文件中的buildVersion。遇到一個問題報錯爲apache
org.xml.sax.SAXParseException; systemId: file:///Users/guoyf/Desktop/Info.plist; lineNumber: 2; columnNumber: 10; 將功能 "http://apache.org/xml/features/disallow-doctype-decl" 設置爲「真」時, 不容許使用 DOCTYPE。
網上查閱發現是由於下載doctype中的dtd對xml進行驗證時,網絡不通所致
解決方法有兩種:
1)去掉info.plist中的 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">,顯然這裏是不可能的。
2)關閉解析器的驗證,不去下載外部dtd文件來對xml進行驗證
第二種方法須要添加的代碼爲
parser=new XmlSlurper()網絡
parser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false) app
parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);ui
以後就能夠拿解析器把plist文件當作XML解析了spa
def customers = parser.parse(new File("文件路徑/Info.plist"))xml
//拿到plist中的dict標籤字符串
def dict = customers.dict;string
//這裏我比較取巧,由於plist文件跟XML不太同樣,取到key的字符串時,找value的字符串不太好找,groovy解析二者關係不太大。io
String version = dict.string[7];//個人info.plist中版本號對應的是第8個string,因此就直接取值了List