已经为元素 "xxx" 指定了绑定到命名空间 "xx" 的属性 "x"

Attribute "x" bound to namespace "xx" was already specified for element "xxx"

我有一些测试代码片段:

import groovy.xml.XmlUtil
import groovy.xml.StreamingMarkupBuilder
class Greet {
def name
Greet(who) { name = who[0].toUpperCase() +
     who[1..-1] }
def salute() { 
     println "Hello !" 
     def input = """
        <manifest xmlns:android="http://schemas.android.com/apk/res/android">
        <application xmlns:android="http://schemas.android.com/apk/res/android"
            android:txt="this is origin">
            <activity android:name="me.aolphn.MainActivity"/>
            <activity xmlns:android="http://schemas.android.com/apk/res/android" android:configChanges ="me.aolphn.SecondActivity"/>

        </application>
        </manifest>
        """
    def root = new XmlParser(false, true).parseText(input)
    //def root = new XmlSlurper(false, true).parseText(input).declareNamespace(android:"http://schemas.android.com/apk/res/android")
    //def writer = new StringWriter()
    //root.'application'.attributes().put('@android:txt1','t1')
    root.'application'[0].'activity'.each{act->
      act.attributes()['android:configChanges']='txt aa'
    }

    println("========xxxxx:\n"+
    XmlUtil.serialize(root))
    //print writer.toString()
}
}

g = new Greet('world')  // create object
g.salute()

如果我运行在here在线,上面的代码会遇到一些异常,报错信息如下:

groovy.lang.GroovyRuntimeException: org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 96; Attribute "configChanges" bound to namespace "http://schemas.android.com/apk/res/android" was already specified for element "activity".
    at Greet.salute(Script1.groovy:29)
    at Greet$salute.call(Unknown Source)
    at Script1.run(Script1.groovy:35)

如何解决这个异常?请帮我。任何输入将不胜感激。

几个小时后,我自己修好了,这是我需要的正确方法。

import groovy.xml.XmlUtil 
import groovy.xml.StreamingMarkupBuilder 
class Greet { 
def name 
Greet(who) { 
name = who[0].toUpperCase() +      who[1..-1] 
} 
def hello() {     
    println "Hello !"       
    def input = """         
    <manifest xmlns:android="http://schemas.android.com/apk/res/android">
        <application xmlns:android="http://schemas.android.com/apk/res/android" 
            android:txt="this is origin">  
            <activity android:name="me.aolphn.MainActivity"/> 
            <activity xmlns:android="http://schemas.android.com/apk/res/android" 
                android:name ="me.aolphn.SecondActivity"
                android:configChanges="origin config xxx"/>
        </application>
    </manifest> 
    """  
    def parser = new XmlSlurper(false,true)
    def root = parser.parseText(input) 
    root.'application'[0].'activity'.each{act-> 

        String value = act.@'android:configChanges'
        println("check value:$value")
        if(value == null||value.isEmpty()){
            act.@'androidconfigChanges'='txt is empty ' 
        }else{
            println("check is invoke ====")
            act.attributes().put('android:configChanges','txt is not null' )
        }
    }
    def xml = XmlUtil.serialize(root)
    root = parser.parseText(xml.replaceAll("androidconfigChanges", "android:configChanges"))
    println("========xxxxx:\n"+     XmlUtil.serialize(root)) 
}  
}
g = new Greet('world')  
g.hello()
​​​​​​​​​​​​​​​​​​​​​​​​​​​

输出结果如下图