2017-02-16 5 views
0

Groovy(特にXmlSlurperimport groovy.xml.XmlUtil)を使用してアンドロイドマニフェストからxmlを読み込もうとしていますが、私はGradleの下にいます。Groovyを使ったアンドロイドXMLの解析エラー

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

をコールする必要がないように

ソリューションは、ルートタグにxmlns:android="http://schemas.android.com/apk/res/android"を追加されました。なぜ解析中に追加していますか? – Rao

+0

こんにちはRao、 "android:name"は私のXMLにあります。 XMLスニペットの先頭にxmlnx:android = ""のような宣言を追加する必要がありますか? –

答えて

0

Raoが指摘したように、私はxml名前空間にバインドできませんでした。そう

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

その後、あなたのxmlに囲まれた、このような名前空間が存在していない.declareNamespace()