kira_codes
kira_codes

Reputation: 1616

namespace error parsing android xml with groovy

I'm trying to read xml from the android manifest using Groovy (particularly XmlSlurper from import groovy.xml.XmlUtil), and I'm getting below from Gradle.

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

The code that results in that error is as follows:

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

I have tried declaring the namespace as follows (from this existing answer)

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

but then I get a more explicit exception for the same namespace issue

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.

Is there anything else I can try?

Upvotes: 0

Views: 620

Answers (1)

kira_codes
kira_codes

Reputation: 1616

As Rao pointed out, I was failing to bound the xml namespace.

The solution was adding xmlns:android="http://schemas.android.com/apk/res/android" to the root tag like so

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

Afterwards there is no need to call .declareNamespace( )

Upvotes: 0

Related Questions