groovy操作xml,獲取屬性值和標籤值並修改
阿新 • • 發佈:2019-01-11
groovy操作xml,獲取屬性值和標籤值,並將其修改
一、用groovy操作xml,修改xml的值
由於我操作的是plist檔案,其中會有IOS的dtd對xml驗證,解析的時候會報錯,如下:
org.xml.sax.SAXParseException; systemId: file:///Users/guoyf/Desktop/Info.plist; lineNumber: 2; columnNumber: 10; 將功能 "http://apache.org/xml/features/disallow-doctype-decl" 設定為“真”時, 不允許使用 DOCTYPE。
解決方法參考:點選這裡吧
話不多說,上程式碼
import groovy.xml.MarkupBuilder import groovy.xml.XmlUtil import groovy.xml.* import java.io.* class XmlWriter { public static main(def args){ /*關閉解析器的驗證,不去下載外部dtd檔案來對xml進行驗證 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> */ def parser = new XmlParser(); parser.setFeature("http://apache.org/xml/features/disallow-doctype-decl",false); parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",false); def file = new File("oa.plist"); def config = parser.parse(file);//解析xml檔案 //獲取xml裡面屬性為version的值,version="1.0" def version = config.attributes().get("version");//1.0 def dict = config.dict; config.attributes().put("version","123");//將屬性值"1.0"改為"123" //列印屬性為name的值,name="雲辦公呀呀呀" println config.attributes().get("name");//雲辦公呀呀呀 // println "${config.attribute('version')}" println config.dict.array.dict.array.dict[0].string[1].text();//獲取標籤值並列印 //修改標籤值 config.dict.array.dict.array.dict[0].string[1].value()[0] = "123456" def xmlFile = "configNewToString.xml"; //用UTF-8寫入,預設為GBK,不然會有亂碼 PrintWriter pw = new PrintWriter(xmlFile,("UTF-8")); // PrintWriter pw = new PrintWriter(xmlFile,("GBK")); pw.write(XmlUtil.serialize(config));//用XmlUtil.serialize方法,將String改為xml格式 pw.close(); } }
附上我要解析的plist原始檔即oa.plist:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0" name="雲辦公呀呀呀"> <dict> <key>items</key> <array> <dict> <key>assets</key> <array> <dict> <key>kind</key> <string>software-package</string> <key>url</key> <string>https://version.geshuinfo.cn:4444/sys/sysAttachment/download?pkid=65d2038e505a432da323f91f409e81ef</string> </dict> <dict> <key>kind</key> <string>display-image</string> <key>needs-shine</key> <true/> <key>url</key> <string>http://ming.pactera.com/mingwebApp/app/logo.5757.png</string> </dict> <dict> <key>kind</key> <string>full-size-image</string> <key>md5</key> <string>baae4e9499e9b29c2420ad8a002f33c2</string> <key>needs-shine</key> <true/> <key>url</key> <string>http://ming.pactera.com/mingwebApp/app/logo.512512.jpg</string> </dict> </array> <key>metadata</key> <dict> <key>bundle-identifier</key> <string>com.chinazw.community</string> <key>bundle-version</key> <string>1.1.0</string> <key>kind</key> <string>software</string> <key>subtitle</key> <string>OA</string> <key>title</key> <string>OA</string> </dict> </dict> </array> </dict> </plist>
然後是通過PrintWriter寫入的configNewToString.xml檔案,其實這個你可以用和原始檔同名的oa.plst這樣就能覆蓋原始檔達到更新的標準了,我的生成檔案如下:
<?xml version="1.0" encoding="UTF-8"?><plist version="123" name="雲辦公呀呀呀">
<dict>
<key>items</key>
<array>
<dict>
<key>assets</key>
<array>
<dict>
<key>kind</key>
<string>software-package</string>
<key>url</key>
<string>123456</string>
</dict>
<dict>
<key>kind</key>
<string>display-image</string>
<key>needs-shine</key>
<true/>
<key>url</key>
<string>http://ming.pactera.com/mingwebApp/app/logo.5757.png</string>
</dict>
<dict>
<key>kind</key>
<string>full-size-image</string>
<key>md5</key>
<string>baae4e9499e9b29c2420ad8a002f33c2</string>
<key>needs-shine</key>
<true/>
<key>url</key>
<string>http://ming.pactera.com/mingwebApp/app/logo.512512.jpg</string>
</dict>
</array>
<key>metadata</key>
<dict>
<key>bundle-identifier</key>
<string>com.chinazw.community</string>
<key>bundle-version</key>
<string>1.1.0</string>
<key>kind</key>
<string>software</string>
<key>subtitle</key>
<string>OA</string>
<key>title</key>
<string>OA</string>
</dict>
</dict>
</array>
</dict>
</plist>
是不是覺得少了點啥?對的,就是少了
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
這個驗證我們解析的時候被關閉了,你可以當做給刪除了。是不是很惱火,正常的xml到此就可以結束了,可是IOS的plist需要它來驗證,這個時候你就不能用解析xml的方法來改值了,下面我提供一個操作檔案的方法來改值
二、操作檔案來改值
程式碼不多,要改的地方按你的需求用正則來寫就可以了,程式碼如下:
//原始檔案
def filePath = "oa.plist"
def file = new File(filePath)
//臨時檔案
def tempFilePath = "oa1.plist"
def tempFile = new File(tempFilePath)
//以"UTF-8"來寫入,不然有漢字亂碼的問題,預設為GBK
def out = tempFile.newPrintWriter("UTF-8")
def lineNum = 1
def newLine = ""
file.eachLine ("UTF-8"){
if (lineNum>2){
//前兩行不做處理,只處理後面的內容
it = it.replace("version=\"1.0\"","version=\"2.0\"")
}
//寫入
out.println(it);
lineNum++
}
//完成寫入
out.flush()
out.close()
以上就我是用來修改xml值的方法,android小白,有大神話還望指教!!!!!!!!!!