我正在尝试使用 Groovy(尤其是 XmlSlurper
中的 import groovy.xml.XmlUtil
)从 android list 中读取 xml,而我正在从 Gradle 获取以下内容。
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')
但后来我得到了同一个命名空间问题的更明确的异常(exception)
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.
还有什么我可以尝试的吗?
最佳答案
正如 Rao 所指出的,我未能绑定(bind) 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( )
关于android - 使用groovy解析android xml的命名空间错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42286684/