android - 使用groovy解析android xml的命名空间错误

标签 android xml gradle groovy android-gradle-plugin

我正在尝试使用 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/

相关文章:

Android:如何坚持 Activity 开始的相同方向

java - Android 转换器应用程序在转换时崩溃

android - 错误 :error: 'drawable/delete.png' is incompatible with attribute android:background (attr) reference|color

javascript - 具有来自 responseXML 的命名空间的跨浏览器 'getElementsByTagName'

android - 标记非敏感数据,以防止从外部来源提交,而不是完全加密? (移动应用程序提交到服务器)

android - 如何在 Flutter 中为按钮添加渐变?

gradle - 将作业参数传递到脚本化管道中的gradle任务

java - 无法直接将我的 .cert 文件 .key 配置为 gretty ssl config

heroku - 尊重部署在 Cloudfoundry 或 Heroku 上的 Gradle/Spring 应用程序的代码库因素(来自 12 因素应用程序声明)

android - TouchImageView 缩放缩放到 picasso 框架