2017-02-16 55 views
0

我想從使用Groovy的Android清單中讀取xml(尤其是XmlSlurperimport groovy.xml.XmlUtil),並且我從Gradle中得到了低於。命名空間錯誤解析android xml與groovy

Error:The prefix "android" for attribute "android:name" associated with an element type "activity" is not bound.

導致該錯誤的代碼如下:

def innerNodeTemplate = ''' 
        <activity android:name=".activity.MyActivity"></activity> 
        ''' 
def activityNode = new XmlSlurper().parseText(innerNodeTemplate) 

我曾嘗試聲明命名空間如下(from this existing answer

activityNode = new XmlSlurper(false,false).parseText(innerNodeTemplate).declareNamespace(android:'android') 

但後來我得到一個更加明確例外情況相同命名空間問題

Error:Cause: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 53; The prefix "android" for attribute "android:name" associated with an element type "activity" is not bound.

還有什麼我可以嘗試嗎?

+0

不存在在XML界這樣的命名空間。爲什麼在解析時添加? – Rao

+0

嗨饒,「android:name」是在我的xml。我是否需要在xml代碼片段的開頭添加像xmlnx:android =「」這樣的聲明? –

回答

0

正如Rao指出的,我沒有綁定xml命名空間。

溶液加入到xmlns:android="http://schemas.android.com/apk/res/android"根標籤,像這樣

def innerNodeTemplate = '''<activity android:name=".activity.MyActivity" xmlns:android="http://schemas.android.com/apk/res/android"></activity>''' 
activityNode = new XmlSlurper(false, true).parseText(innerNodeTemplate) 

之後沒有必要調用.declareNamespace()