已经为元素“ xxx”指定了绑定到名称空间“ xx”的属性“ x”

问题描述 投票:1回答:1

我有一些测试代码片段:

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)

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

xml groovy xml-namespaces xml-attribute
1个回答
0
投票

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

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()
​​​​​​​​​​​​​​​​​​​​​​​​​​​

输出结果如下图

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.