2017-02-16 133 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()